gpt4 book ai didi

windows - 使用Golang获取用户名和域名

转载 作者:行者123 更新时间:2023-12-01 22:13:55 29 4
gpt4 key购买 nike

有什么办法可以使用golang获取Windows用户名?
我也想获得一个域名。在我的情况下,域是“WORKGROUP”

cmd-> systeminfo:

enter image description here

最佳答案

您必须使用os和os/user软件包来处理域和当前用户名

package main

import (
"fmt"
"os"
"os/user"
)

func main() {
fmt.Println(user.Current()) //return current username
fmt.Println(os.Hostname()) //return the hostname(domain)
}

关于windows - 使用Golang获取用户名和域名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61840740/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com