こんにちは、kurumi-bioです。
第1回目のuserパッケージ(標準ライブラリー)の学習です。
前回の記事
APIリファレンス(過去記事の一覧)
環境
- Windows
OSバージョン:Windows11 Home 22H2
Go言語のバージョン:go version go1.20.3 windows/amd64 - Linux
OSバージョン:openSUSE Leap 15.4
Go言語のバージョン:go version go1.20.3 linux/amd64
LookupGroup関数
func LookupGroup(name string) (*Group, error)
package main import ( "fmt" "os" "os/user" ) func main() { if len(os.Args) != 2 { fmt.Fprintln(os.Stderr, "usage: LookupGroup グループ名") os.Exit(1) } g, e := user.LookupGroup(os.Args[1]) if e != nil { fmt.Fprintf(os.Stderr, "LookupGroup Error:%v\n", e) os.Exit(2) } fmt.Printf("Name=[%s]\n", g.Name) fmt.Printf("Gid=[%s]\n", g.Gid) }
◆実行結果(Windows)
◆実行結果(Linux)
◆実行結果(Linux)
LookupGroupId関数
func LookupGroupId(gid string) (*Group, error)
◆テストコード
package main import ( "fmt" "os" "os/user" ) func main() { if len(os.Args) != 2 { fmt.Fprintln(os.Stderr, "usage: LookupGroupId グループID") os.Exit(1) } g, e := user.LookupGroupId(os.Args[1]) if e != nil { fmt.Fprintf(os.Stderr, "LookupGroupId Error:%v\n", e) os.Exit(2) } fmt.Printf("Name=[%s]\n", g.Name) fmt.Printf("Gid=[%s]\n", g.Gid) }
◆実行結果(Windows)
◆実行結果(Linux)
◆実行結果(Linux)
GroupIds関数
func (u *User) GroupIds() ([]string, error)
Current関数
func Current() (*User, error)
◆テストコード
package main import ( "fmt" "os" "os/user" ) func getGroupName(gid string) string { n, e := user.LookupGroupId(gid) if e != nil { fmt.Fprintf(os.Stderr, "LookupGroupId Error[%s]:%v\n", gid, e) return "" } return n.Name } func main() { u, e := user.Current() if e != nil { fmt.Fprintf(os.Stderr, "Current Error:%v\n", e) os.Exit(1) } fmt.Printf("Gid=[%s]:[%s]\n", u.Gid, getGroupName(u.Gid)) fmt.Printf("HomeDir=[%s]\n", u.HomeDir) fmt.Printf("Name=[%s]\n", u.Name) fmt.Printf("Uid=[%s]\n", u.Uid) fmt.Printf("Username=[%s]\n", u.Username) l, e2 := u.GroupIds() if e2 != nil { fmt.Fprintf(os.Stderr, "GroupIds Error:%v\n", e2) os.Exit(2) } for c, s := range l { fmt.Printf("GroupId[%d]=[%s]:[%s]\n", c, s, getGroupName(s)) } }
◆実行結果(Windows)
◆実行結果(Linux)
Lookup関数
func Lookup(username string) (*User, error)
◆テストコード
package main import ( "fmt" "os" "os/user" ) func main() { if len(os.Args) != 2 { fmt.Fprintln(os.Stderr, "usage: Lookup ユーザー名") os.Exit(1) } u, e := user.Lookup(os.Args[1]) if e != nil { fmt.Fprintf(os.Stderr, "Lookup Error:%v\n", e) os.Exit(2) } fmt.Printf("Gid=[%s]\n", u.Gid) fmt.Printf("HomeDir=[%s]\n", u.HomeDir) fmt.Printf("Name=[%s]\n", u.Name) fmt.Printf("Uid=[%s]\n", u.Uid) fmt.Printf("Username=[%s]\n", u.Username) l, e2 := u.GroupIds() if e2 != nil { fmt.Fprintf(os.Stderr, "GroupIds Error:%v\n", e2) os.Exit(3) } for c, s := range l { fmt.Printf("GroupId[%d]=[%s]\n", c, s) } }
◆実行結果(Windows)
◆実行結果(Linux)
◆実行結果(Linux)
LookupId関数
func LookupId(uid string) (*User, error)
◆テストコード
package main import ( "fmt" "os" "os/user" ) func main() { if len(os.Args) != 2 { fmt.Fprintln(os.Stderr, "usage: LookupId ユーザーID") os.Exit(1) } u, e := user.LookupId(os.Args[1]) if e != nil { fmt.Fprintf(os.Stderr, "LookupId Error:%v\n", e) os.Exit(2) } fmt.Printf("Gid=[%s]\n", u.Gid) fmt.Printf("HomeDir=[%s]\n", u.HomeDir) fmt.Printf("Name=[%s]\n", u.Name) fmt.Printf("Uid=[%s]\n", u.Uid) fmt.Printf("Username=[%s]\n", u.Username) l, e2 := u.GroupIds() if e2 != nil { fmt.Fprintf(os.Stderr, "GroupIds Error:%v\n", e2) os.Exit(3) } for c, s := range l { fmt.Printf("GroupId[%d]=[%s]\n", c, s) } }
◆実行結果(Windows)
◆実行結果(Linux)
◆実行結果(Linux)
最後までご覧いただきありがとうございます