打开和写入文件时出错
Error in opening and writing to a file
我想打开一个文件并向其中写入一些文本,但是出现以下错误:
.\hello.go:13: cannot use msg (type string) as type []byte in argument to f.Write
到目前为止,这是我的代码:
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")
}
使用 io.WriteString(f, msg)
、f.Write([]byte(msg))
或 io.Copy(f, strings.NewReader(msg))
。
我想打开一个文件并向其中写入一些文本,但是出现以下错误:
.\hello.go:13: cannot use msg (type string) as type []byte in argument to f.Write
到目前为止,这是我的代码:
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")
}
使用 io.WriteString(f, msg)
、f.Write([]byte(msg))
或 io.Copy(f, strings.NewReader(msg))
。