你好!以下是你要翻译的内容: 如何在Go语言中检测操作系统版本?

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

How Can I Detect the OS Version in Go?

问题

我目前正在编写一个REST API客户端,并且我正在尝试确定用户使用的操作系统,以便生成一个有用的用户代理字符串。这将使我能够(希望能够)在将来做出关于应该支持哪些操作系统的良好决策。

我想要做的是生成一个类似于以下的字符串:

Linux/1.3.2 或 Darwin/1.3.2 或 Windows/1.3.2

它应该适用于Linux/Mac/Windows。

目前,我能够使用以下代码获取操作系统类型(例如:'linux' / 'windows' / 'darwin'):

runtime.GOOS

但是我找不到一种方法来获取操作系统的主/次/微版本号。

英文:

I'm currently writing a REST API client, and I'm trying to determine what OS the user is using in order to generate a useful User Agent string. This is going to allow me to (hopefully) make good decisions about what OS's I should bother supporting in the future.

What I'd like to do is this: generate a string that looks something like:

Linux/1.3.2 OR Darwin/1.3.2 OR Windows/1.3.2

It should work across Linux / Mac / Windows.

Currently, I'm able to retrieve the OS type (eg: 'linux' / 'windows' / 'darwin') using

runtime.GOOS

But I'm unable to find a way to grab the OS major/minor/micro versions.

答案1

得分: 4

根据在Go Google Group中的广泛讨论,似乎没有可靠的解决方案。我仍然建议您查看讨论,因为其中有一些有用的想法,可以提取特定操作系统的一些信息。

P.S. 浏览器是否在User_Agent中发送了这些信息?

User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36
英文:

Based on the extensive discussion in Go google group it looks like nothing reliable exists. I still suggest you to take a look at the discussion, as there are some useful ideas how to extract some information for particular OS.

P.S. does not the browser send this information in User_Agent?

User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36

答案2

得分: 1

使用uname(用于uname(2)系统调用)函数,该函数应该在像Linux和MacOSX这样的POSIX系统上可用。

我不明白为什么你需要这么精确的信息;顺便说一下,在Linux和可能的MacOSX上,这还不够,libc的版本(当然,还有GO运行时)至少同样重要。

(如果你关心的话,你应该在Windows上找到类似的东西)

英文:

Use uname (for the uname(2) syscall), which should be available on POSIX systems like Linux & MacOSX.

I don't understand why you need such precise information; BTW, on Linux, and probably MacOSX, it is in not enough, the version of the libc (and of course, of the GO runtime) matters at least as much.

<sup>(You should find something similar for Windows if you care)</sup>

huangapple
  • 本文由 发表于 2015年5月25日 14:09:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/30432169.html
匿名

发表评论

匿名网友

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

确定