Creating files and directories
This recipe describes a few general ways you can create files and directories in code.
How to do it...
- Open the console and create the folder
chapter06/recipe07
. - Navigate to the directory.
- 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) } }
- Execute the code by running
go run create.go
in the main Terminal. - List the content...