golang write string to file
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer f.Close()
if _, err = f.WriteString(text); err != nil {
panic(err)
}
golang write string to file
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer f.Close()
if _, err = f.WriteString(text); err != nil {
panic(err)
}
golang read file
dat, err := ioutil.ReadFile("/tmp/dat")
check(err)
fmt.Print(string(dat))
golang bufio write to file
package main
import (
"bufio"
"log"
"os"
)
func main() {
sampledata := []string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"Nunc a mi dapibus, faucibus mauris eu, fermentum ligula.",
"Donec in mauris ut justo eleifend dapibus.",
"Donec eu erat sit amet velit auctor tempus id eget mauris.",
}
file, err := os.OpenFile("test.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("failed creating file: %s", err)
}
datawriter := bufio.NewWriter(file)
for _, data := range sampledata {
_, _ = datawriter.WriteString(data + "\n")
}
datawriter.Flush()
file.Close()
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us