[Golang] Write Lines to File
Write a list of strings (lines) to file in Go. First use strings.Join to convert lines to a single string. Then use ioutil.WriteFile to write the single string to file.
import (
"io/ioutil"
"strings"
)
func WriteLinesToFile(lines []string, filename string) (err error) {
return ioutil.WriteFile(filename, []byte(strings.Join(lines, "\n")+"\n"), 0644)
}
If you want to know how to read lines from file, see [1].
Tested on: Ubuntu Linux 17.10, Go 1.10.1.
References:
[1] | [Golang] Read Lines From File or String |