Writing a file is an essential task for every programmer; Go supports multiple ways on how you can do this. This recipe will show a few of them.
Writing the file
How to do it...
- Open the console and create the folder chapter06/recipe03.
- Navigate to the directory.
- Create the writefile.go file with the following content:
package main
import (
"io"
"os"
"strings"
)
func main() {
f, err := os.Create("sample.file")
if err != nil {
panic(err)
}
defer f.Close()
_, err = f.WriteString("Go is awesome!\n")
if err != nil {
panic(err)
}
...