Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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.Username
dclong
currentUser.Uid
1000
currentUser.Gid
1000

Test An User’s Access to a Path

Use unix.Access

import "golang.org/x/sys/unix"
unix.Access("/home", unix.R_OK) == nil
true
unix.Access("/home", unix.W_OK)
permission denied
unix.Access("/tmp", unix.W_OK) == nil
true

Use 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