In Go, you can change the permissions of a file or directory using the os.Chmod
function from the os
package. Here's an example:
package main
import (
"fmt"
"os"
)
func main() {
filePath := "path/to/file.txt" // Path to the file or directory you want to change permissions for
// Change the permissions of the file or directory to read-only for all users
err := os.Chmod(filePath, 0444)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Permissions changed successfully!")
}
In this example, we use os.Chmod
to change the permissions of the file or directory specified by the filePath
variable. The second argument 0444
is an octal representation of the permission bits. For example, 0444
sets read-only permissions for all users. You can use different values depending on the specific permission configuration you want.
If the os.Chmod
function returns an error, we print it out. Otherwise, we print a success message.
Make sure you have the necessary permissions to change the permissions of the file or directory.