Last active
November 22, 2021 19:45
-
-
Save melekhine/d6bb100cef6d1ef5040fb920f2a3dbad to your computer and use it in GitHub Desktop.
Write lines to a text file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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