Append String to File in Golang

Create new folder named src. In src folder, create new folder named data. In data folder, create new text file named a.txt as below:

Hello World

In src folder, create new file named main.go as below:

package main

import (
	"fmt"
	"os"
)

func main() {

	file, err := os.OpenFile("data/a.txt", os.O_APPEND|os.O_WRONLY, 0666)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Fprintln(file, "Hi")
		fmt.Println("Done")
	}
	file.Close()

}




Open Terminal windows in Visual Studio Code and run command line: go run main.go

Done