gpt4 book ai didi

go - 无法通过 golang 获取 Uname

转载 作者:IT王子 更新时间:2023-10-29 01:22:00 30 4
gpt4 key购买 nike

我在 Mac 上使用 golang 1.4.2

我想使用 Uname要获取一些信息,以下是我的代码:

package main

import (
"syscall"
)

func main() {
utsname := syscall.Utsname{}
syscall.Uname(&utsname)
}

但是我得到了这些错误:

# command-line-arguments
./main.go:8: undefined: syscall.Utsname
./main.go:9: undefined: syscall.Uname

哪里出错了?

最佳答案

TL;DR UnameUtsname 不适用于 OSX。

原因是因为那些函数没有为操作系统定义。

阅读 syscall 的文档时,我突然想到了:

The details vary depending on the underlying system, and by default, godoc will display the syscall documentation for the current system. If you want godoc to display syscall documentation for another system, set $GOOS and $GOARCH to the desired system.

在我的 Mac 上运行 godoc syscall 生成的 sycall 文档不包括 Utsname 类型和 Uname 函数调用。

但是,运行 GOOS=linux GOARCH=amd64 godoc syscall 实际上会显示 UtsnameUname

另外,请注意包本身被锁定以支持特定于操作系统的包

https://golang.org/pkg/syscall/ => https://godoc.org/golang.org/x/sys

关于go - 无法通过 golang 获取 Uname,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29415909/

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