#Spread Empty Interface meaning?

11 messages · Page 1 of 1 (latest)

static rivet
visual pike
#

also the things its returning is still int, error because it's calling Fprintf and that returns int, error

static rivet
#

@visual pike Thanks

can you explain why I HAVE to pass &buffer instead of just passing the buffer itself into Greet() ??

    buffer := bytes.Buffer{}
    Greet(&buffer, "Chris")

    got := buffer.String()
    want := "Hello, Chris"

    if got != want {
        t.Errorf("got %q want %q", got, want)
    }

func Greet(writer io.Writer, name string) {
    fmt.Fprintf(writer, "Hello, %s", name)
}
}```
visual pike
static rivet
#

cannot use buffer (variable of type bytes.Buffer) as io.Writer value in argument to Greet: bytes.Buffer does not implement io.Writer (method Write has pointer receiver) How does this not impliment io.Writer when passed as a variable but it does when referenceed as a pointer??

visual pike
static rivet
#

buffer := bytes.Buffer{} //create variable with an empty buffer
fmt.Println(buffer)//Empty
Greet(&buffer, "Chris") //fill the buffer
fmt.Println(&buffer)// {[72 101 108 108 111 44 32 67 104 114 105 115] 0 0}
fmt.Println(buffer) //Hello, Chris

I'm not sure how to interpret this behavior or why the pointer would implement io.Writer() but the variable (which is a copy inside Greet()) doesn't.

visual pike
static rivet
#

Still don't understand. I guess ill just make it a rule that a Writer is always a pointer and leave it at that.