无法通过golang获取Uname

huangapple go评论95阅读模式
英文:

Cannot get Uname by golang

问题

我正在使用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

出了什么问题?

英文:

I'm using golang 1.4.2 on Mac

I want to use Uname to get some information, followings are my codes:

package main

import (
  "syscall"
)

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

But I got these errors:

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

Any went wrong?

答案1

得分: 8

TL;DR UnameUtsname在OSX上不可用。

原因是这些函数在该操作系统中没有定义。

阅读syscall的文档时,我注意到了这一点:

> 具体细节取决于底层系统,并且默认情况下,godoc将显示当前系统的syscall文档。如果您希望godoc显示另一个系统的syscall文档,请将$GOOS和$GOARCH设置为所需的系统。

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

然而,运行GOOS=linux GOARCH=amd64 godoc syscall实际上显示了UtsnameUname

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

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

英文:

TL;DR Uname and Utsname are not available for OSX.

The reason is because those functions are not defined for the operating system.

Reading the documentation for syscall this jumped at me:

> 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.

Running godoc syscall on my Mac yielded the sycall documentation which does not include the Utsname type nor the Uname function call.

However, running GOOS=linux GOARCH=amd64 godoc syscall actually shows the Utsname and Uname.

Also, note that the package itself is locked down in favor of OS specific packages

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

答案2

得分: 1

(将代码部分翻译为中文)

代替导入syscall,我导入了golang.org/x/sys/unix

然后,下面的代码块:

u := syscall.Utsname{}
syscall.Uname(&u)

被替换为:

u := unix.Utsname{}
unix.Uname(&u)

注意!你需要调用适用于Windows的相关函数。

git diff的相关内容如下:

-       "syscall"
+       "golang.org/x/sys/unix"

-       utsname := syscall.Utsname{}
-       syscall.Uname(&utsname)
+       utsname := unix.Utsname{}
+       unix.Uname(&utsname)
英文:

(Adding a more direct answer with exactly how I got past this problem)

Instead of importing syscall, I imported golang.org/x/sys/unix.

Then, the following code-block:

u := syscall.Utsname{}
syscall.Uname(&u)

is replaced by:

u := unix.Utsname{}
unix.Uname(&u)

NOTE! You need to call relevant functions for Windows.

Relevant contents of git diff:

-       "syscall"
+       "golang.org/x/sys/unix"

-       utsname := syscall.Utsname{}
-       syscall.Uname(&utsname)
+       utsname := unix.Utsname{}
+       unix.Uname(&utsname)

huangapple
  • 本文由 发表于 2015年4月2日 22:48:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/29415909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定