I want to open a file and write some text to it, however I get the following error:
.\hello.go:13: cannot use msg (type string) as type []byte in argument to f.Write
Here's my code so far:
package main
import (
"os"
)
func printer(msg string) (err error) {
f, err := os.Create("helloworld.txt")
if err != nil {
return err
}
defer f.Close()
f.Write(msg)
return err
}
func main() {
printer("Hello World")
}
Use io.WriteString(f, msg)
, f.Write([]byte(msg))
or io.Copy(f, strings.NewReader(msg))
.