简体   繁体   中英

How Can I Detect the OS Version in Go?

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.

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.

PS 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

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.

(You should find something similar for Windows if you care)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM