I am reading the source code for Go's io package and I came across a snippet I don't fully understand. Here it is
func WriteString(w Writer, s string) (n int, err error) {
if sw, ok := w.(stringWriter); ok {
return sw.WriteString(s)
}
return w.Write([]byte(s))
}
where
type stringWriter interface {
WriteString(s string) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
The type assertion w.(stringWriter)
asserts that the dynamic type of w (i.e. Writer) implements the stringWriter interface. I don't see how this is possible given the type definitions of stringWriter and Writer. Assuming that this code is correct, what is it that I am missing?