Golang os.Open, os.Chmod File ExamplesUse the os.Open func to get a file descriptor. See syntax and file system errors.
dot net perls
Os.Open. In Golang a common task is to open a file. We must get a file handle (or descriptor) from the os.Open func, part of the "os" package.
Some errors, like "syntax is incorrect" errors or "cannot find file" errors, may be expected. We can test the second result value from os.Open to see any errors.
Open, syntax is incorrect. Here we pass in a file name that has incorrect syntax. The operating system does not support file names with a question mark in them.
Golang program that uses os.Open, syntax incorrect
package main import ( "fmt" "os" ) func main() { // This file path is invalid. f, err := os.Open("test?") if err != nil { fmt.Println("Error occurred") fmt.Println(err) return } fmt.Println(f) }
Error occurred open test?: The filename, directory name, or volume label syntax is incorrect.
Cannot file the file. If we call os.Open and the file is not present on the system, we will also get an error. We can detect this by checking that the error is not nil.
Tip This error will occur in normal program operation. It is a good reason to test the error return value of os.Open.
Golang program that uses os.Open, file not found
package main import ( "fmt" "os" ) func main() { // This file does not exist. f, err := os.Open(`C:\perls\vvv`) if err != nil { fmt.Println(err) return } fmt.Println(f) }
Error occurred open C:\perls\vvv: The system cannot find the file specified.
Read file. We almost always will want to use the file descriptor returned by os.Open. In this example we pass it to the ioutil.ReadAll func.
And We convert the result to a string. And then we print the string to the console.
Golang program that uses os.Open, reads file
package main import ( "io/ioutil" "fmt" "os" ) func main() { // Open a file. f, err := os.Open(`C:\programs\file.txt`) if err != nil { fmt.Println(err) return } // Pass file descriptor returned by os.Open to ioutil.ReadAll. result, _ := ioutil.ReadAll(f) // Convert byte slice to string. fmt.Println(string(result)) }
Thank you Friend
Chmod sets the permissions for a file. In macOS files sometimes cannot be accessed due to lack of Read permissions. By calling Chmod with a special code, we can give it permissions.
Golang program that uses os.Chmod, sets permissions
package main import ( "fmt" "os" ) func main() { name := "/Users/sam/svg.html" // Set as readable and writable. // Pass 0 to remove all permissions. os.Chmod(name, 0755) // Done. fmt.Println("DONE") }
Unknown escape sequence. Paths in Go must encode the backslash character—we can use double backslashes, or just use tick-syntax for strings.
String Literal
Causes error:
f, err := os.Open("C:\programs\file.txt")
# command-line-arguments C:\programs\file.go:13:27: unknown escape sequence
f, err := os.Open(`C:\programs\file.txt`)
A summary. It is important to use both the file object, and the error return value, when calling os.Open. Many problems will cause an error to be non-nil.
© 2007-2021 sam allen. see site info on the changelog