Ad Code

How to Write or Append into a File in Golang


    Hello friends! In this post, I will be discussing about how we can write or append into a file.

The whole code will look like below:

package main

import (

func main() {
// file, err := os.Create("test.txt")
file, err := os.OpenFile("main.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal("Oops! Some error: ", err.Error())
defer file.Close()

_, er := file.WriteString(" and Java")
if er != nil {
log.Fatal("Oops! Some error: ", er.Error())