I understand the general concept of Go's interfaces. However, I was recently looking into implementing the io.Reader
interface, and it has me confused. I found this post which didn't help to much.
Reader interface and the Read method in golang
To start with, the accepted answer is using io.Reader
's Read
function, which as far as I can tell is never implemented. Second, how does the Read
function work in the context of something like ioutil.ReadAll
. It takes something that implemented the io.Reader
interface and returns a slice of bytes. I don't understand how something that is returning only an int
and err
can be processed into a slice of bytes.
Edit:
I was helped in the go-nuts IRC channel and this was the provided answer as to how you would likely actually implement one http://play.golang.org/p/ejpUVOx8jR. Much thanks to the go community.
Edit 2:
as pointed out below the implementation above will fail in the case where the strign is larger than the buffer. This is a more sane implementation http://play.golang.org/p/t4Zg8TnF33.