On Sat, Apr 28, 2018 at 8:56 AM <k1atti...@gmail.com> wrote:

> I want to search in all library (which have source code)

If the above means the standard library: either local godoc search or
https://golang.org/search?q=Read. But as you can see, you'll get a vast
amount of hits.

> for exmaple :
> I see this. This function need an interface type : r
> func NewScanner(r io <https://golang.org/pkg/io/>.Reader
<https://golang.org/pkg/io/#Reader>) *Scanner
<https://golang.org/pkg/bufio/#Scanner>
>
> But which type satisfy this r parameter ?

The answer is: the type of the instance you pass to NewScaner.

You want to use scanner.Scanner to scan something. What is something? Say
it's the standard input. Does os.Stdin implement io.Reader? It does,
because it's an os.File that has the Read method with a compatible
signature. Problem solved, we're done. Same for any other file.

But maybe you have a different "something" you want to scan. Say it's a
[]byte. Then task is to add a Read([]byte) (int, error) method to []byte?
It's a nice exercise to do by yourself. (One must use a named type for
that, BTW). However, someone must have ran into the same problem before for
sure, right? Of course, package bytes has a helper that constructs an
io.Reader from a []byte: bytes.NewBuffer. And if a string needs to be
scanned there's strings.NewBuffer.

In general, the task is to made one's "something" implement the required
interface. That some type other than "something" implements the interface
may or may not be helpful, but the useful property of interfaces is that
one can always make "something"  (provided it's a named type) directly
implement the interface.

-- 

-j

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to