I'm not sure how to formulate the question properly. I have 2 go code snippets that should do the exact same thing but apparently they don't, since one works and the other doesn't (doesn't compile)
func writeSomething(writer *io.Writer) {
}
func main() {
file, _ := os.Create("error.log")
var logWriter io.Writer = file
writeSomething(&logWriter)
}
func main2() {
file, _ := os.Create("error.log")
writeSomething(&file)
}
main() works and main2() doesn't.
prog.go:20:17: cannot use &file (type **os.File) as type *io.Writer in argument to writeSomething: *io.Writer is pointer to interface, not interface
The only difference is that I used an intermediate variable but I'm not doing any pointer referencing or dereferencing.
What am I doing wrong here?
mainyou convertfileto interfaceio.Writerand then passwriteSomethinga pointer to the interface which is just whatwriteSomethingexpects. - md2perpemain2you have a pointer toos.Fileand then pass a pointer to that pointer towriteSomething. Go cannot convert that to a pointer to interfaceio.Writer. - md2perpe