Golang Program to open a file in write-only mode


In Golang, we can use OS functions os.OpenFile(), os.Create() and ioutil.WriteFile to open a write-only file. Here we are going to see three different Examples to understand the functionality of these functions. In the first program we will use the OpenFile() function while in the second and third program, we will use the writeFile() and create() function respectively.

Syntax

func WriteFile(filename string, data []byte, perm os.FileMode) error

The WriteFile() function is present in the ioutil package and is used to write the data in the file. The function accepts the name of file in which data is to be written along with the data slice of byte and the permission as arguments to it.

os.Openfile()

This function is a part of os package. It is used to open a file to read. It takes one input i.e. the filename which will be opened.

os.create()

This function comes from the os package. It helps in the creation of a new file. The filename is given as the input in the function.

Algorithm

  • Import the Required Package

  • Create the main function

  • Use the internal function to Open the file

  • Print if the file is opened or not

Example 1

In this Example, we will write a go language program to open a file in write-only mode by using the functions present in the os package.

package main

import (
   "fmt"
   "os"
)

func main() {
   file, err := os.OpenFile("sample.txt", os.O_WRONLY|os.O_CREATE, 0644)
   if err != nil {
      panic(err)
   } else {
      fmt.Println("The file is successfully opened in write-only mode")
   }
   defer file.Close()
}

Output

The file is successfully opened in write-only mode

Example 2

In this Example, we will write a go language program to open a file in write-only mode by using the functions present in os package.

package main

import (
   "fmt"
   "os"
)

func main() {
   file, err := os.Create("newfile.txt")
   if err != nil {
      panic(err)
   } else {
      fmt.Println("The file is successfully opened in write-only mode")
   }
   defer file.Close()
}

Output

The file is successfully opened in write-only mode

Example 3

In this Example, we will write a go language program to open a file in write-only mode by using a WriteFile() function present in ioutil package.

package main

import (
   "fmt"
   "io/ioutil"
)

func main() {
   err := ioutil.WriteFile("newfile.txt", []byte("Hello, World!"), 0644)
   if err != nil {
      panic(err)
   } else {
      fmt.Println("The file is successfully opened in write-only mode")
   }
}

Output

The file is successfully opened in write-only mode

Conclusion

We have successfully compiled and executed a go language program to open a file in write-only mode along with Examples. Here in this article, we learned how to open a file in write-only mode using different Examples in Go. We saw how to use os.OpenFile, os.Create, and ioutil.WriteFile to achieve this.

Updated on: 03-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements