Photo by AbsolutVision on Unsplash
Golang bad file descriptor (how to append to a file in Go)
bad file descriptor, how to write to a file in Golang, using `os` package
Play this article
Golang updating a file
When you use Golang, Its really a good idea, playing with files is actually fun, however this popular error, occurs when you want to update or append to a file, without deleteing the text written already.
Golang creating a file
This is only possible if you want to write to the file for the first time.
f, err := os.Create(fileName)
if err != nil {
return err
}
_, err = f.WriteString("text")
if err != nil {
return err
}
Golang updating a file
This is how you open a file, and append to it.
f, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)
// the args: os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm
//f, err := os.Create(fileName)
if err != nil {
return err
}
_, err = f.WriteString("text")
if err != nil {
return err
}
for more inforamtions please referr to this stackoverflow link.