Go user包

king1076 · · 2124 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

user的结构:

type User struct {
    Uid      string // user id
    Gid      string // primary group id
    Username string
    Name     string
    HomeDir  string
}

user包中的主要函数:

    type User
        func Current() (*User, error)
        func Lookup(username string) (*User, error)
        func LookupId(uid string) (*User, error)

事例1  windows:

package main

import (
	"fmt"
	"os/user"
)

func main() {
	usr, error := user.Current()
	if error == nil {
		fmt.Println(usr)
	}
}

[ `go run testuser.go` | done: 7.5934343s ]
	&{S-1-5-21-2450167352-2372647358-1749370957-1000 S-1-5-21-2450167352-2372647358-1749370957-513 City-PC\City  C:\Users\City}

事例2 windows:


package main

import (
	"fmt"
	"os/user"
)

func main() {
	usr, error := user.Current()
	if error == nil {
		fmt.Println(usr.Uid)
		fmt.Println(usr.Gid)
		fmt.Println(usr.Username)
		fmt.Println(usr.Name)
		fmt.Println(usr.HomeDir)
		_usr, error1 := user.Lookup(usr.Username)
		if error1 == nil {
			fmt.Println(_usr)
		} else {
			fmt.Println(error1.Error())
		}
	} else {
		fmt.Println(error.Error())
	}
}
[ `go run testuser.go` | done: 14.3608214s ]
	S-1-5-21-2450167352-2372647358-1749370957-1000
	S-1-5-21-2450167352-2372647358-1749370957-513
	City-PC\City
	
	C:\Users\City
	&{S-1-5-21-2450167352-2372647358-1749370957-1000 unknown City-PC\City  Unknown directory}
事例3 windows:

package main

import (
	"fmt"
	"os/user"
)

func main() {
	usr, error := user.Current()
	if error == nil {
		fmt.Println(usr.Uid)
		fmt.Println(usr.Gid)
		fmt.Println(usr.Username)
		fmt.Println(usr.Name)
		fmt.Println(usr.HomeDir)
		_usr, error1 := user.Lookup(usr.Uid)
		if error1 == nil {
			fmt.Println(_usr)
		} else {
			fmt.Println(error1.Error())
		}
	} else {
		fmt.Println(error.Error())
	}
}

[ `go run testuser.go` | done: 7.5694329s ]
	S-1-5-21-2450167352-2372647358-1749370957-1000
	S-1-5-21-2450167352-2372647358-1749370957-513
	City-PC\City
	
	C:\Users\City
	No mapping between account names and security IDs was done.



好像uid查询 在windows上不怎么起作用

包的bug提示:

 Lookup and LookupId functions do not set Gid and HomeDir fields in the User struct returned on windows.


Linux,Fedora19

code:

package main
import(
        "fmt"
        "os/user"
)
func PrintUsr(usr *user.User){
        fmt.Println("------------------------------")
        fmt.Println("Uid: ",usr.Uid)
        fmt.Println("Gid: ",usr.Gid)
        fmt.Println("Username: ",usr.Username)
        fmt.Println("Name: ",usr.Name)
        fmt.Println("HomeDir: ",usr.HomeDir)
}
func main(){
        usr,_ := user.Current()
        PrintUsr(usr)
        user1,_:=user.Lookup(usr.Name)
        PrintUsr(user1)
        user2,_:=user.LookupId(usr.Uid)
        PrintUsr(user2)
}

Result:

[jjy@localhost user]$ go run testuser.go
------------------------------
Uid:  1000
Gid:  1000
Username:  jjy
Name:  jjy
HomeDir:  /home/jjy
------------------------------
Uid:  1000
Gid:  1000
Username:  jjy
Name:  jjy
HomeDir:  /home/jjy
------------------------------
Uid:  1000
Gid:  1000
Username:  jjy
Name:  jjy
HomeDir:  /home/jjy



有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:king1076

查看原文:Go user包

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2124 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传