Golang Program to open a file in append mode, open if the file does not exist


In golang we can os.Openfile function of os package and ioutil.WriteFile() function of ioutil package to append a file. Then, we will use WriteString() function to print the result. In this article, we are going to learn how to create a golang programt to open a file in append mode incase the file does not exist.

Syntax

os.OpenFile(name/path string, flag int, perm FileMode)

The OpenFile() function is present in os package. The function accepts three arguments. One is the name of the file that is to be opened followed by the int type instruction used to open a file. This signifies the mode in which the file is to be opened i.e read only, write only, read-write mode, etc. The function returns two values as the result one is the file on which different operations such as write or append can be performed based on the file mode passed to the function along with the error file.

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.

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.

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 the functions of os packages. To open a file in append mode, we will be using the "os.OpenFile" function.

package main

import (
   "fmt"
   "os"
)

func main() {
   // Open the file in append mode
   file, err := os.OpenFile("newfile.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)
   } 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 various functions present in ioutil package. We will be using the WriteFile() function of ioutil package to implement the result.

package main
import (
   "fmt"
   "io/ioutil"
)

func main() {
   // Write to the file
   err := ioutil.WriteFile("sample.txt", []byte("Hello, World!\n"), 0644)
   if err != nil {
      fmt.Println(err)
   } 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 along with Examples. We have looked at two Examples: using the "os" package and using the "ioutil" package. Both Examples provide a high-level interface for working with files and make it easy to perform file I/O operations.

Updated on: 03-May-2023

263 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements