使用 Golang 获取用户名和域名 [英] Get username and domain using Golang

查看:79
本文介绍了使用 Golang 获取用户名和域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用 golang 获取 Windows 用户名?我也想要一个域名.在我的情况下域是WORKGROUP"

is there any way to get Windows User name using golang? I would like to get a Domain too. In my case Domain is "WORKGROUP"

cmd -> 系统信息:

cmd -> systeminfo:

推荐答案

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

You must us os and os/user packages to handle domain and current username

package main

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

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

这篇关于使用 Golang 获取用户名和域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆