Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
Get Information of the Current User¶
user.Current() returns information of the current (real) user.
If the code is run by a non-root user named some_user on Linux,
then information of some_user is returned.
However,
if some_user has sudo access and run the code using sudo,
then the real user user is root
and the information of root is returned.
import "os/user"currentUser, err := user.Current()currentUser&{1000 1000 dclong dclong /home/dclong}currentUser.UsernamedclongcurrentUser.Uid1000currentUser.Gid1000Test An User’s Access to a Path¶
Use unix.Access¶
import "golang.org/x/sys/unix"unix.Access("/home", unix.R_OK) == niltrueunix.Access("/home", unix.W_OK)permission deniedunix.Access("/tmp", unix.W_OK) == niltrueUse os.OpenFile + os.IsPermission¶
This way only works on files. Please refer to Golang : Test file read write permission example for detailed discussions.
import "os"os.OpenFile("/home", os.O_WRONLY, 0666)<nil> open /home: is a directory