Golang program to create a new file


Golang has two internal function – os.create and ioutil.WriteFile to create a new file. The "file" is a disk-based file that a Go program may read from or write to. In Go, the OS can be used to represent a file. os package file type that offers ways to open, read from, write to, and manipulate files.

Method 1: Using os.Create Command

In this method, the os.Create function uses the specified name to create a new file or truncate an existing one. When the function has completed running, the file is closed using the defer command.

Syntax

Os.Create

In Go programming language, create is a part of os package , this function creates a new file, it contains a single parameter i.e. the filename which is to be created.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and os package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Use the os.Create in the main function to create a new file with the specified name. Both a *File type and an error value are returned by the function.

  • Step 3 − Verify that the error value is not zero. Print the error and exit the function if it is not nil.

  • Step 4 − When the function has completed running, use the defer statement to close the file.

  • Step 5 − To show that the file was successfully generated, print a message using fmt.println() function where ln means new line.

Example

In this example, we will use os.Create function to create a new file. Let’s see through the code.

package main
import (
   "fmt"
   "os"
)

//create main function to execute the program
func main() {
   file, err := os.Create("myfile.txt")  //create a new file
   if err != nil {
      fmt.Println(err)
      return
   }
   defer file.Close()
   fmt.Println("File is created successfully.")  //print the success on the console
}

Output

File is created successfully.

Method 2: Using ioutil.WriteFile Function

In this method, the file name, the byte slice that will be written to the file, and the file mode are the three arguments required by the function. In this instance, 0644 stands for a Unix-style file mode that grants the owner read, write, and read access while granting everyone else read permissions. Compared to the os.Create method, this one is clearer and easier to use, but it has less control over the file that is created.

Syntax

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

  • Step 1 − Create a package main and declare fmt(format package) and io/ioutil package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Use the ioutil.WriteFile function in the main to create a new file with the given name and the given byte slice can be written to it. An error value is returned by the function.

  • Step 3 − Verify that the error value is not zero. Print the error and exit the function if it is not nil.

  • Step 4 − To show that the file was successfully generated, print a message.

Example

In this example, we will use ioutil.WriteFile to create a new file. Let’s have a look at the code.

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

//create main function to execute the program
func main() {
   err := ioutil.WriteFile("myfile.txt", []byte("Hello, alexa!"), 0644) //create a new file
   if err != nil {
      fmt.Println(err)
      return
   }
   fmt.Println("File is created successfully.") //print the success on the console
}

Output

File is created successfully.

Conclusion

We executed the program of creating a new file using two examples. In the first example we used os.Create command and in the second example we used ioutil.WriteFile command to execute the program.

Updated on: 21-Feb-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements