Check if the file exists
Create the log.txt file in the same directory of this program to detect.
package main import ( "os" "fmt" ) func main() { if _, err := os.Stat("log.txt"); err == nil { fmt.Println("Log.txt file exists") } }
Check if the file does not exist
package main import ( "os" "fmt" ) func main() { // IsNotExist function definition: func IsNotExist (error) bool // Returns a boolean indicating whether the error indicates that a file or directory does not exist if _, err := os.Stat("log.txt"); os.IsNotExist(err) { fmt.Println("Log.txt file does not exist") } else { fmt.Println("Log.txt file exists") } }
Read file contents
Create the name.txt file in the same directory of this program to detect.
package main import ( "io/ioutil" "fmt" ) func main() { contentBytes, err := ioutil.ReadFile("name.txt") if err == nil { // ReadFile returned [] byte fmt.Println(string(contentBytes)) } }
write file
Create a hello world file in the same directory as this program.
package main import ( "io/ioutil" "fmt" ) func main() { hello := "Hello, World" // WriteFile takes three parameters: the file name to write, the [] byte to write, and the write permission. err := ioutil.WriteFile("hello_world", []byte(hello), 0644) if err != nil { fmt.Println(err) } }
Create temporary file
package main import ( "io/ioutil" "fmt" ) func main() { helloWorld := "Hello, World" // TempFile definition: func TempFile (DIR, prefix string) (f * os.file, error error) // Create a new temporary file with prefix in dir directory. // Open the file in read-write mode and return the os.File pointer. file, err := ioutil.TempFile("", "hello_world_temp") if err != nil { panic(err) } // defer os.Remove(file.Name()) if _, err := file.Write([]byte(helloWorld)); err != nil { panic(err) } fmt.Println(file.Name()) }
Calculate file lines
Create "names.txt" file in the same directory of the program, and write a few lines at random.
package main import ( "os" "bufio" "fmt" ) func main() { file, _ := os.Open("names.txt") fileScanner := bufio.NewScanner(file) lineCount := 0 for fileScanner.Scan() { lineCount++ } defer file.Close() fmt.Println(lineCount) }
Read file specific lines
package main import ( "os" "bufio" "fmt" ) func main() { fmt.Println(readLine(2)) } func readLine(lineNumber int) string { file, _ := os.Open("names.txt") fileScanner := bufio.NewScanner(file) lineCount := 0 for fileScanner.Scan() { if lineCount == lineNumber { return fileScanner.Text() } lineCount++ } defer file.Close() return "" }
Compare the contents of two files
package main import ( "fmt" "io/ioutil" "bytes" ) func main() { one, err := ioutil.ReadFile("one.txt") if err != nil { panic(err) } two, err2 := ioutil.ReadFile("two.txt") if err2 != nil { panic(err2) } // Equal returns the bool value same := bytes.Equal(one, two) fmt.Println(same) }
Delete files
package main import "os" func main() { err := os.Remove("new.txt") if err != nil { panic(err) } }
Copy or move files
package main import ( "os" "io" ) func main() { original, err := os.Open("original.txt") if err != nil { panic(err) } defer original.Close() original_copy, err2 := os.Create("copy.txt") if err2 != nil { panic(err2) } defer original_copy.Close() // Copy (1, 2) - > copy 2 to 1 _, err3 := io.Copy(original_copy, original) if err3 != nil { panic(err3) } } // Move file: use os.Remove() to delete the original file after copying
rename file
package main import "os" func main() { os.Rename("old.txt", "new.txt") }
Delete directory and file
package main import ( "os" "fmt" ) func main() { // Remove cannot delete a non empty folder. RemoveAll can err := os.RemoveAll("hello") if err != nil { panic(err) } }
List all files in the directory
Create a hello folder. Create two files in the folder. The main file and the Hello folder are side by side.
. ├── hello │ ├── 1.txt │ └── 2.txt └── main.go
package main import ( "io/ioutil" "fmt" ) func main() { files, err := ioutil.ReadDir("hello") if err != nil { panic(err) } for _, f := range files { fmt.Println(f.Name()) } }