英文:
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 Uname
和Utsname
在OSX上不可用。
原因是这些函数在该操作系统中没有定义。
阅读syscall
的文档时,我注意到了这一点:
> 具体细节取决于底层系统,并且默认情况下,godoc将显示当前系统的syscall文档。如果您希望godoc显示另一个系统的syscall文档,请将$GOOS和$GOARCH设置为所需的系统。
在我的Mac上运行godoc syscall
会显示不包括Utsname
类型和Uname
函数调用的sycall
文档。
然而,运行GOOS=linux GOARCH=amd64 godoc syscall
实际上显示了Utsname
和Uname
。
另外,请注意,该包本身已被锁定,以支持特定于操作系统的包。
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论