How to write file in Go
Here is the basic way to write a file in go using the ioutil go module. The WriteFile function requires the file name, the data to write as a []byte, and the permission. As output there is the status of the operation. If the output is nil, the file was successfully written, otherwise you can inspect the variable content and see the error to fix. The permission fs.ModePerm will get the 0777 permission (access/write/read).
Code
err := os.WriteFile(FILE NAME, data, fs.ModePerm)
Example: write a file with ioutil
write_file.go
package main
import (
"io/fs"
"os"
)
func main() {
data := []byte("hello world\n\ngenerated by go")
if err := os.WriteFile("text_file.txt", data, fs.ModePerm); err != nil{
panic(err)
}
}