Append to a file in Go

File IoGo

File Io Problem Overview


So I can read from a local file like so:

data, error := ioutil.ReadFile(name)

And I can write to a local file

ioutil.WriteFile(filename, content, permission)

But how can I append to a file? Is there a built in method?

File Io Solutions


Solution 1 - File Io

This answers works in Go1:

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)
}

Solution 2 - File Io

Go docs has a perfect example :

package main

import (
	"log"
	"os"
)

func main() {
	// If the file doesn't exist, create it, or append to the file
	f, err := os.OpenFile("access.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		log.Fatal(err)
	}
	if _, err := f.Write([]byte("appended some data\n")); err != nil {
		log.Fatal(err)
	}
	if err := f.Close(); err != nil {
		log.Fatal(err)
	}
}

Solution 3 - File Io

Figured it out

More info

f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0644) 

n, err := f.WriteString(text) 

f.Close()

Solution 4 - File Io

... I would use fmt.Fprintf, because accept a writer... and a connection or files will be a writer and easy to write in a way of string...

f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
    panic(err)
}

defer f.Close()
fmt.Fprintf(f, "%s", text)

I hope this help!

Javier,

Solution 5 - File Io

If you also want to create the file

f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)

Solution 6 - File Io

f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
    panic(err)
}

defer f.Close()

if _, err = f.WriteString(text); err != nil {
    panic(err)
}

Made a slight change from the one provided in the golang site by adding flags to the os.OpenFile function which by default will only allow reading of a file and not editing amongst other features.

Solution 7 - File Io

Let's say you want to add the contents of filecurrent to the file all, then below code is working:

func updateTrx() {
	var err error
	var f *os.File

	// If the file doesn't exist, create it, or append to the file
	if f, err = os.OpenFile("all.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err := f.Close(); err != nil {
			log.Fatal("error", err)
		}
	}()

	var current *os.File
	if current, err = os.OpenFile("current.csv", os.O_RDONLY, 0); err != nil {
		log.Fatal("error", err)
	}

	defer func() {
		if err := current.Close(); err != nil {
			log.Fatal("error", err)
		}
	}()

	if fileBytes, err := ioutil.ReadAll(current); err != nil {
		log.Fatal("error", err)
	} else {
		if _, err := f.Write([]byte(fileBytes)); err != nil {
			log.Fatal(err)
		}
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionseveibarView Question on Stackoverflow
Solution 1 - File IoSridhar RatnakumarView Answer on Stackoverflow
Solution 2 - File IoJimmy Obonyo AborView Answer on Stackoverflow
Solution 3 - File IoseveibarView Answer on Stackoverflow
Solution 4 - File IoJavier Gutiérrez-Maturana SáncView Answer on Stackoverflow
Solution 5 - File IoLuke WView Answer on Stackoverflow
Solution 6 - File Ioparadox earthlingView Answer on Stackoverflow
Solution 7 - File IoHasan A YousefView Answer on Stackoverflow