How to create a file in go

Oct 5, 2018 10:11 · 91 words · 1 minute read

Go lets you easily create and write a new file.

Here is an example using create from os package.

package main

import "os"

func main() {
	//create a file
	f, err := os.Create("foo")
	if err != nil {
		panic(err)
	}
	//make sure it closes.
	defer f.Close()
}

os.Create creates a file with default mode 0666 , and default file descriptor as O_RDWR .

Here is another explicit example

package main

import "os"

func main() {
	f, err := os.OpenFile("foo", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		panic(err)
	}
	defer f.Close()
}