# Golang bad file descriptor (how to append to a file in Go)


## 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.
```go
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.

```go
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
}
```

![bad-file-descriptor.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1662064116961/wdSC-lluW.png align="left")


for more inforamtions please referr to [this stackoverflow link](https://stackoverflow.com/questions/33851692/golang-bad-file-descriptor).

