打开和写入文件时出错

Mor*_*a R -2 io file go

我想打开一个文件并写一些文本,但是我收到以下错误:

.\hello.go:13: cannot use msg (type string) as type []byte in argument to f.Write
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是我的代码:

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")
}
Run Code Online (Sandbox Code Playgroud)

Cal*_*leb 5

使用io.WriteString(f, msg),f.Write([]byte(msg))或io.Copy(f, strings.NewReader(msg)).