http://golang.org/search?q=Read
It would be cool to have a code search that searches for types with a
specific method set. It would be challenging to implement, what with
type embedding etc. Worth looking into, though.
> 2. Apart from reading byte by byte, what is the best way to convert
> type string to something that implements io.Reader.
Use bytes.Buffer, which implements Reader:
b := bytes.NewBufferString("your string")
Andrew
-rob
It would be cool, indeed.
However, although I love the duck-typing of interfaces as much as
anybody else, I have noticed that many times you implement interfaces
on purpose. It would be nice if that was reflected in the
documentation *and* it was checked by the compiler at compile time.
I propose to use an Implements function to do this. For example, if I
want MyType to implement the io.ReaderWriter interface I would write:
func (mt MyType)Implements() io.ReaderWriter {
return io.ReaderWriter(mt)
}
That function would be a convention, but no language changes are
needed. This way, the compiler would tell me if MyType does not
implement ReaderWriter and it would appear clearly in the
documentation.
Of course, this does not solve the problem of knowing all the types
which implement an interface, but if this convention was followed
writing the tool that Andrew is suggesting would not be such a
challenging task.
--
- yiyus || JGL . 4l77.com
Why do that, when you can already do this:
type A interface { Foo() }
]type T struct {}
func (t T) Foo() {}
var _ = A(T{}) // fail to compile if T doesn't satisfy A
> Of course, this does not solve the problem of knowing all the types
> which implement an interface, but if this convention was followed
> writing the tool that Andrew is suggesting would not be such a
> challenging task.
It's not worth adding complexity to the language just to make it
marginally easier to write tools like this.
Andrew
Um, yy's suggestion doesn't add anything to the language. It adds
a convention for use.
One can argue about which of the alternative conventions are
more obvious / compact / greppable / whatever, but those are
different arguments.
Chris
--
Chris "allusive" Dollin
Because this won't appear in the package documentation. I want to say
to the people using my package: "I am implementing this interface".
While I think not having to do this is a good thing, I don't know
what's the problem with doing it.
> It's not worth adding complexity to the language just to make it
> marginally easier to write tools like this.
This would not add any complexity to the language (it would just be a
convention that would require no language change) and the intention is
not to "make marginally easier" to write any tool, but to explicitly
say to the compiler and the users that a type is implementing an
interface. I will accept the argument that go does not want to make
this explicit, although I don't fully understand why.
You could also add documentation that said "I am implementing this interface".
You're right; in my haste I didn't read closely enough. Apologies yy.
Regardless, I'm not impressed by the suggested convention (which has
been discussed before). I understand its merits, but IMO it just adds
noise. I'd rather just have a good tool for matching interface
signatures.
Andrew
If you run
godoc strings Reader
or visit http://golang.org/pkg/strings/#Reader
you'll find that the Read method is on *Reader
so either you need to construct and pass a *Reader
on your own, or you can call the constructor
function NewReader also listed in that documentation.
Russ
This won't work if you want to declare that MyType implements more
than one interface, since methods can't be overloaded.
type A interface { a() }
type B interface { b() }
type C interface { c() }
type X int
func (X) a() {}
func (X) b() {}
func (X) c() {}
func (x X) Implements() (A, B, C) {
return x, x, x
}
-rob