Answers for "golang open file for write"

Go
3

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)
}
Posted by: Guest on November-17-2021
0

golang writefile example

err := os.WriteFile("index.txt", []byte("example writefile using go"), os.ModePerm)
if err != nil {
  fmt.Println(err)
}

res, _ := os.ReadFile("index.txt")
fmt.Println(string(res))
Posted by: Guest on October-04-2021

Browse Popular Code Answers by Language