Changing file ownership
This program will take the file provided and change the user and group ownership. This could be used in tandem with the example that finds files that you have permission to modify.
Go provides os.Chown()
in the standard library, but it does not accept string values for the user and group names. The user and group must be provided as integer ID values. Fortunately, Go also comes with an os/user
package that contains functions for looking up an ID based on a name. These functions are user.Lookup()
and user.LookupGroup()
.
You could look up your own user and group information on Linux/Mac with the id
, whoami
, and groups
commands.
Note that this does not work on Windows because ownership is treated differently. The following is the code implementation of this example:
package main import ( "fmt" "log" "os" "os/user" "strconv" ) func main() { // Check command line arguments if len(os.Args) != 4 { fmt.Println("Change the owner of a file.") ...