Skip to content

Instantly share code, notes, and snippets.

@melekhine
Last active November 22, 2021 19:45
Show Gist options
  • Save melekhine/d6bb100cef6d1ef5040fb920f2a3dbad to your computer and use it in GitHub Desktop.
Save melekhine/d6bb100cef6d1ef5040fb920f2a3dbad to your computer and use it in GitHub Desktop.
Write lines to a text file
package main
import (
"fmt"
"os"
)
// Sentence will be wrote as line in file
type Sentence string
func main() {
const filename = "sentences.txt"
const fflags = os.O_WRONLY | os.O_APPEND | os.O_CREATE
const fmode = 0o666
fp, err := os.OpenFile(filename, fflags, fmode)
if err != nil {
fmt.Println("File opening error:", err)
}
defer fp.Close()
lines := []Sentence{
"IT'S A LUCK",
"TO BE",
"WITH NVIDIA NOT APART",
}
for _, line := range lines {
n, err := fmt.Fprintln(fp, line)
if err != nil {
fmt.Println("Error: ", err, "while write to file", filename)
}
fmt.Println(n, "bytes written to", filename)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment