Golang Program to open a file in read-write mode with truncating file


In Golang, the functions os.openfile () and ioutil.Writefile() are used to access/open a file in read-write mode without truncating it. In this article, we will understand the functionality of these two functions using two different methods. In the first method we will use the OpenFile() function present in os package and In the second method, we will use the WriteFile() function to implement the result.

Syntax

os.OpenFile()

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 desired package

  • Open the file in main function using built-in functions

  • Print whether the file is opened or not

Example 1

In this Example we will write a go language program to open a file in read-write mode with truncating file by using various functions present in os package.To open a file in read-write mode with truncating its contents, we can use the "os.OpenFile" function this function opens the required file and flag.

package main

import (
   "fmt"
   "os"
)

func main() {
   // Open the file in read-write mode with truncating its contents
   file, err := os.OpenFile("newfile.txt", os.O_RDWR|os.O_TRUNC|os.O_CREATE, 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("We have successfully opened the file in read-write mode")
   }
}

Output

We have successfully opened the file in read-write mode

Example 2

In this Example we will write a go language program to open a file in read-write mode by using various functions present in ioutil package. The "ioutil" package contains 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("sample.txt", []byte("Hello, World!\n"), 0644)
   if err != nil {
      fmt.Println(err)
      return
   } else {
      fmt.Println("The file is successfully opened in read-write mode")
   }
}

Output

The file is successfully opened in read-write mode

Conclusion

We have successfully compiled and executed a go language program to open a file in read-write mode along with Examples.We did this by using "os" and the "ioutil" packages, depending on the level of control you need over the file and the desired ease of use. Both Examples provide a simple and straightforward way to write data to a file and truncate its contents and the file will be created by its own if it does not exist.

Updated on: 03-May-2023

119 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements