Checking read and write permissions

Similar to the previous example, checking the read and write permissions is done by inspecting an error using a function called os.IsPermission(). This function will return true if the error passed was caused due to a permission problem, as shown in the following example:

package main import ( "log" "os" ) func main() { // Test write permissions. It is possible the file // does not exist and that will return a different // error that can be checked with os.IsNotExist(err) file, err := os.OpenFile("test.txt", os.O_WRONLY, 0666) if err != nil { if os.IsPermission(err) { log.Println("Error: Write permission denied.") } } file.Close() // Test read permissions file, err = os.OpenFile("test.txt", os.O_RDONLY, ...

Get Security with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.