Os/user Package usage in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Os/user package allows user accounts to find users by user name or user ID

Type Unknownusererror

Type Unknownusererror string

Func (e unknownusererror) error () string//When a user cannot be found through lookup, the error is returned. Type Unknownuseriderror

Type Unknownuseriderror int

Func (e unknownuseriderror) error () string////returns the error when a user ID cannot be found through lookup.

Type User

Type user struct {Uid      string//User Idgid      string//Primary group Idusername string  Name     stringhomedir
  string      //Household Directory}

Func current () (*user, error)//Getting information about the user
Func lookup (Username string) (*user, error)//Find user information by user name
Func lookupid (UID string) (*user, error)//Find user information based on user ID.

Func main () {usr, err: = user. Current () if err! = Nil {fmt. PRINTLN (Err)}fmt. Println (usr. Gid) fmt. Println (usr. Homedir) fmt. Println (usr. Name) fmt. Println (usr. Uid) fmt. Println (usr. Username) usr, _ = user. Lookup ("root")//based on user name, find the users fmt. Println (usr) usr, err = user. LookupId ("1100")//Find user FMT based on UserID. Println (usr, err)}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.