How do I truncate and completely rewrite a file without having leading zeros?

See the documentation on Truncate:

Truncate changes the size of the file. It does not change the I/O offset. If there is an error, it will be of type *PathError.

So you also need to seek to the beginning of the file before you write:

configFile.Truncate(0)
configFile.Seek(0,0)

As shorthand, use the flag os.O_TRUNC when calling os.OpenFile to truncate on open.


I'd like to show an alternative for cases where you may just want to truncate the file, then another option may be the following:

func truncate(filename string, perm os.FileMode) error {
    f, err := os.OpenFile(filename, os.O_TRUNC, perm)
    if err != nil {
        return fmt.Errorf("could not open file %q for truncation: %v", filename, err)
    }
    if err = f.Close(); err != nil {
        return fmt.Errorf("could not close file handler for %q after truncation: %v", filename, err)
    }
    return nil
}

Tags:

Go