Golang Program to open a file in append mode


In Go programming language, os and io packages can be used for performing various operations on external files like copying, editing, and even appending them. In this article, we will learn two Examples to open a file in append mode. In the first Example we will use os.OpenFile function from os package and in the second Example we will use ioutil.WriteFile function from ioutil package.

Syntax

os.OpenFile("test.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

The OpenFile() function is present in os package and is used to open a file. The function accepts the file to be opened as argument to the function along with "os.O_APPEND|os.O_CREATE|os.O_WRONLY", which is a constant indicating that the file should be opened in append mode, created if it does not exist, and opened for writing only.0644, which are the Unix-style permissions for the file.

file.WriteString()

In Go programming language, WriteString method is used to write a string to a file. The string is passed as an argument in the function as str.

ioutil.WriteFile()

In Go, WriteFile belongs to ioutil package and contains three parameters, the first is the filename where data is to be written, second is the data which is to be written and third is the file permission. The data is written to the file if function is executed successfully.

Algorithm

  • Import the required packages.

  • Create a main function

  • Open the file in append mode in main using built-in function

  • Print whether the file is successfully opened or not

Example 1

In this Example we will write a go language program to open a file in append mode by using various functions present in os package. The first method involves using the "os" package, which provides a low-level interface for manipulating the operating system's underlying file descriptors. To open a file in append mode, we can use the "os.OpenFile" function, which opens a file with the specified name andflags.

package main

import (
   "fmt"
   "os"
)

func main() {
   // Open the file in append mode
   file, err := os.OpenFile("notepad.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
   if err != nil {
      fmt.Println(err)
      return
   }
   defer file.Close()

   // Write to the file
   _, err = file.WriteString("Hello, World!\n")
   if err != nil {
      fmt.Println(err)
      return
   } else {
      fmt.Println("The file is successfully opened in append mode")
   }
}

Output

The file is successfully opened in append mode

Example 2

In this Example we will write a go language program to open a file in append mode by using the ioutil package. The second method for opening a file in append mode involves using the "ioutil" package, which provides a higher-level interface for reading and writing files. The "ioutil" package includes a "WriteFile" function that can be used to write data to a file, and it will automatically create the file if it does not exist.

package main

import (
   "fmt"
   "io/ioutil"
)

func main() {
   // Write to the file
   err := ioutil.WriteFile("newfile.txt", []byte("Hello, World!\n"), 0644)
   if err != nil {
      fmt.Println(err)
      return
   } else {
      fmt.Println("The file is successfully opened in append mode")
   }
}

Output

The file is successfully opened in append mode

Conclusion

We have successfully compiled and executed a go language program to open a file in append mode using the two Examples. In the first Example we used the os package and in the second Example we used ioutil package.

Updated on: 03-May-2023

242 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements