Golang Program to create an empty file


In Golang, os and io packages are used for performing various file-r operations like editing, copying, creating, and deleting. In this article, we are going to see three different Examples to create an empty file. In the first Example we will use os.Create from the os package, in the second Example we will use WriteFile function from ioutil package is used and in the third Example we will use NewWriter function from bufio package is used.

Syntax

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.

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 in the program

  • Create a main function

  • In the main create an empty file using built-in functions

  • Print the whether the empty file is created or not

Example 1

In this Example, we will create a new file using theos package which provides several functions for working with the file system, including the creation of a new file.

package main

import (
   "fmt"
   "os"
)

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

Output

The given file is opened in read-only mode

Example 2

In this Example, we will write a go language program to create an empty file using the ioutil package.The ioutil package provides several utility functions for working with I/O, including the creation of a new file.

package main

import (
   "fmt"
   "io/ioutil"
)

func main() {
   err := ioutil.WriteFile("file.txt", []byte{}, 0644)
   if err != nil {
      panic(err)
   } else {
      fmt.Println("Successfully created the empty file")
   }
}

Output

Successfully created the empty file

Example 3

In this Example we will write a go language program to create an empty file by using various functions present in bufio package. The bufio package provides buffered I/O operations, and we can use it to create a new file.

package main

import (
   "bufio"
   "os"
   "fmt"
)

func main() {
   file, err := os.Create("file.txt")
   if err != nil {
      panic(err)
   }else {
      
      defer file.Close()

      writer := bufio.NewWriter(file)
      writer.Flush()
      fmt.Println("Successfully created the empty file")
   }
}

Output

Successfully created the empty file

Conclusion

We have successfully compiled and executed a go language program to create an empty file there are several ways to create an empty file in Golang, using the os, ioutil, and bufio packages. Each method provides a different set of capabilities and trade-offs, and you can choose the method that best fits your use case.

Updated on: 03-May-2023

357 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements