io.Reader and os.File

3,498 views
Skip to first unread message

Wolfgang

unread,
Aug 6, 2010, 2:18:01 PM8/6/10
to golang-nuts
Hi--

i have the following bit of test code:

package fastq

import (
"testing"
"os"
"fmt"
)

var testfileName string = "test.fq"

func TestEchoFastq(t *testing.T) {
tagsFile, err := os.Open(testfileName, os.O_RDONLY, 0)
tags := ParseFastq(tagsFile)
if err == nil {
t.Fatal("Could not open testfile %s", testfileName)
}
for t := range tags {
fmt.Fprint(os.Stderr, t)
}
}

where ParseFastq is defined as 'func ParseFastq(input *io.Reader)
chan<- *tag.Tag'.

The compiler complains:

cannot use tagsFile (type *os.File) as type *io.Reader in function
argument

From the documentation, it seems that the os.File should fulfill the
io.Reader interface. What am i doing wrong here?

I'm new to go, so please excuse me if this is something obvious that i
missed.

Thanks,
Wolfgang

Evan Shaw

unread,
Aug 6, 2010, 2:24:02 PM8/6/10
to Wolfgang, golang-nuts

You want ParseFastq to accept an argument of type io.Reader, not
*io.Reader. *os.File implements io.Reader, but *os.File cannot be
treated as a pointer to an io.Reader.

When passing around interfaces, you typically pass by value because
the interfaces contain either a pointer or a primitive type.

- Evan

Reply all
Reply to author
Forward
0 new messages