Creating files and directories

Part of Golang Mastery course

~15 min read
Interactive
Hands-on
Beginner-friendly
  • few general ways you can create files and directories in code.

Create the create.go file with the following content:#

package main import ( "os" ) func main() { f, err := os.Create("created.file") if err != nil { panic(err) } f.Close() f, err = os.OpenFile("created.byopen", os.O_CREATE|os.O_APPEND, os.ModePerm) if err != nil { panic(err) } f.Close() err = os.Mkdir("createdDir", 0777) if err != nil { panic(err) } err = os.MkdirAll("sampleDir/path1/path2", 0777) if err != nil { panic(err) } }

output:

sangam:golang-daily sangam$ create.go sangam:golang-daily sangam$ tree . ā”œā”€ā”€ binary ā”œā”€ā”€ config.json ā”œā”€ā”€ content.dat ā”œā”€ā”€ created.byopen ā”œā”€ā”€ created.file ā”œā”€ā”€ createdDir ā”œā”€ā”€ data.csv ā”œā”€ā”€ data.xml ā”œā”€ā”€ data.zip ā”œā”€ā”€ example.txt ā”œā”€ā”€ flatfile.txt ā”œā”€ā”€ main.go ā”œā”€ā”€ sample.file ā”œā”€ā”€ sample.txt ā”œā”€ā”€ sampleDir │   └── path1 │   └── path2

How it works...#

  • The previous example represents four ways you can create a file or directory. The os.Create function is the simplest way to create the file. By using this function, you will create the file with permissions such as 0666.

  • If you need to create the file with any other configuration of permissions, then the OpenFile function of the os package is the one to be used.

  • The directories can be created by using the Mkdir function of the os package. This way, a directory with given permissions is created. The second option is to use the MkdirAll function. This function also creates the directory, but if the given path contains non-exiting directories, then all directories in the path are created (it works the same as the -p option of Unix's mkdir utility).

Your Progress

98 of 103 modules
95%
Started95% Complete
←Previous
SpaceComplete
→Next