Welcome To Golang By Example

Create an empty file in Go (Golang)

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

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