Table of Contents
Overview
os.Create() can be used to create an empty file in go. The signature of the function is
func Create(name string) (*File, error)
Basically this function
- Create a named file with mode 0666
- It truncates the file if it already exits
- In case of path issue, it returns a Path error
- It returns a file descriptor which can be used for both reading and write
Code:
package main
import (
"log"
"os"
)
func main() {
file, err := os.Create("emptyFile.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
}
Output:
Check the contents of the file. It will be empty