The Wayback Machine - https://web.archive.org/web/20240718032940/https://www.geeksforgeeks.org/how-to-create-an-empty-file-in-golang/
Open In App

How to Create an Empty File in Golang?

Last Updated : 02 Apr, 2020
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Like other programming languages, Go language also allows you to create files. For creating a file it provides Create() function, this function is used to create or truncates the given named file.

  • This method will truncate the file, if the given file is already exists.
  • This method will create a file with mode 0666, if the given file doesn’t exist.
  • If the given path is not correct, then this method will throw an error of type *PathError.
  • This method return a file descriptor which can be used for both reading and write.
  • It is defined under the os package so, you have to import os package in your program for accessing Create() function.

Syntax:

func Create(file_name string) (*File, error)

Example 1:




// Golang program to illustrate how to create
// an empty file in the default directory
package main
  
import (
    "log"
    "os"
)
  
func main() {
  
    // Creating an empty file
    // Using Create() function
    myfile, e := os.Create("GeeksforGeeks.txt")
    if e != nil {
        log.Fatal(e)
    }
    log.Println(myfile)
    myfile.Close()
}


Output:

creating an empty file in golang

Example 2:




// Golang program to illustrate how to create
// an empty file in the new directory
package main
  
import (
    "log"
    "os"
)
  
func main() {
  
    // Creating an empty file
    // Using Create() function
    myfile, e := os.Create("/Users/anki/Documents/new_folder/GeeksforGeeks.txt")
    if e != nil {
        log.Fatal(e)
    }
    log.Println(myfile)
    myfile.Close()
}


Output:

creating an empty file in golang in specified directory



Similar Reads

Checking if structure is empty or not in Golang
If the structure is empty means that there is no field present inside that particular structure. In Golang, the size of an empty structure is zero. Whenever the user wants to know if the created structure is empty or not, he can access the structure in the main function through a variable. If there doesn't exist any field inside the structure, he c
3 min read
How to Rename and Move a File in Golang?
In the Go language, you are allowed to rename and move the existing file to a new path with the help of the Rename() method. This method is used to rename and move a file from the old path to the new path. If the given new path already exists and it is not in a directory, then this method will replace it. But OS-specific restrictions may apply when
2 min read
How to Delete or Remove a File in Golang?
In the Go language, you are allowed to remove the existing file with the help of the Remove() method. This method removes the specified file from the director or it also removes empty directories. If the given path is incorrect, then it will throw an error of type *PathError. It is defined under the os package so, you have to import os package in y
1 min read
How to Truncate a File in Golang?
In Go language, you are allowed to truncate the size of the file with the help of the Truncate() function. This function is used to truncate the size of the given file in the specified size. If the given file is a symbolic link, then it changes the size of the link's target. If this method throws an error, then it will be of type *PathError. It is
2 min read
Checking the Given File Exists or Not in Golang
In Go language, you are allowed to check whether the given file exists or not with the help of the IsNotExist() function. If this function returns true, then it indicates that the error is known to report that the specified file or directory already does not exist and if it returns false, then it indicates that the given file or directory exists. T
2 min read
How to Read a File Line by Line to String in Golang?
To read a file line by line the bufio package Scanner is used. Let the text file be named as sample.txt and the content inside the file is as follows: GO Language is a statically compiled programming language, It is an open-source language. It was designed at Google by Rob Pike, Ken Thompson, and Robert Grieserner. It is also known as Golang. Go la
2 min read
How to Uncompress a File in Golang?
As per Wikipedia, data compression or file compression can be understood as a process of reducing the size of a particular file/folder/whatever data while still preserving the original data. Lesser file size has a lot of benefits as it will then occupy lesser storage area giving you more space for other data to occupy, transfer the file faster as t
6 min read
Compressing a File in Golang
As per Wikipedia, data compression or file compression can be understood as a process of reducing the size of a particular file/folder/whatever data while still preserving the original data. Lesser file size has a lot of benefits as it will then occupy lesser storage area giving you more space for other data to occupy, transfer the file faster as t
5 min read
How to Read File Word By Word in Golang?
File reading is such an important aspect of a programming language. We need to perform certain operations programmatically, using automation in file reading/writing allows us to create certain programs/projects which might have looked impossible otherwise. File InputTo work with files, we have to input an existing file as we are reading from a file
5 min read
How to Check File Size and Last Modified Time in Golang?
In the Go language, the os module provides the ability to access native operating-system features. In the os package, the Stat() function is used to check the file size and last modified time. This method gives the FileInfo structure describing the file and an error of type *PathError. // FileInfo describes a file and is returned by the stat type F
2 min read
three90RightbarBannerImg