API to get the user's OS and OS version

7,296 views
Skip to first unread message

Amitabh Arya

unread,
Mar 19, 2012, 6:16:01 PM3/19/12
to golan...@googlegroups.com
Hi,

       Is there any APIs, which can tell user's system Operating system and it's version and other information about system. I checked the os package, but didn't find one.

Brad Fitzpatrick

unread,
Mar 19, 2012, 6:17:55 PM3/19/12
to Amitabh Arya, golan...@googlegroups.com
No, nothing like that is in the standard library.

Amitabh Arya

unread,
Mar 19, 2012, 6:19:54 PM3/19/12
to Brad Fitzpatrick, golan...@googlegroups.com
Any ways to figure out the user's current system. So that you can defined path or write/read files on particular path.

Thanks,
Amitabh Arya

Brad Fitzpatrick

unread,
Mar 19, 2012, 6:22:21 PM3/19/12
to Amitabh Arya, golan...@googlegroups.com
Oh, if all you need is the current OS, there's runtime.GOOS, which is "linux", "darwin", "windows", etc.

But that doesn't tell you the OS version.

Hotei

unread,
Mar 19, 2012, 6:51:04 PM3/19/12
to golan...@googlegroups.com
You can check all the system's variables using  os pkg.  The standard environment may have something useful but is not guaranteed. HOWEVER -  if you can control what gets put in the users environment then this might work. The Go team has gradually reduced how much stuff is required to be manually entered in the environment but a few years ago it was a bit more.  Updating environment info is no fun from the "user support" side, but for a small group in a stable situation it might work.

Amitabh Arya

unread,
Mar 19, 2012, 6:55:12 PM3/19/12
to Brad Fitzpatrick, golan...@googlegroups.com
I think,  GOOS  only provide the set environment variable, nothing else. So if I have installed go on my system. Then yes it will work fine. But any other user, who does not have "Go lang" installed, this will not work on his system. Ass GOOS is from runtime pkg.

Correct me if I am wrong please.

Thanks,
Amitabh Arya

Amitabh Arya

unread,
Mar 19, 2012, 6:57:58 PM3/19/12
to Hotei, golan...@googlegroups.com
Thanks for your reply Hotei :). But no I tried  os.Environ() before posting this post. It mainly give you the all set variable in environment. Just path nothing information about OS. :(
Correct me if I am wrong. 


Thanks,
Amitabh Arya

Ian Lance Taylor

unread,
Mar 19, 2012, 7:07:23 PM3/19/12
to Amitabh Arya, Brad Fitzpatrick, golan...@googlegroups.com
Amitabh Arya <amitab...@gmail.com> writes:

> I think, GOOS only provide the set environment variable, nothing else. So
> if I have installed go on my system. Then yes it will work fine. But any
> other user, who does not have "Go lang" installed, this will not work on
> his system. Ass GOOS is from runtime pkg.

runtime.GOOS is the operating system for which the Go compiler and
packages were built. It's affected by the environment variable you use
when you build Go, but it's not affected by the environment when the Go
program is run.

Ian

brainman

unread,
Mar 19, 2012, 7:57:13 PM3/19/12
to golan...@googlegroups.com, Brad Fitzpatrick
On Tuesday, March 20, 2012 9:55:12 AM UTC+11, Amitabh Arya wrote:

Correct me if I am wrong please.


As Brad said, runtime.GOOS global variable will hold your answer. If it has "linux", then your program is running on Linux, "windows" - Windows, and so on. This will tell you your OS name. If you need more, like "version", you need to be more specific. There is no single definition of "os version", these varies between different OS. For example, I use Windows, so I would use something like that:

package main

import (
    "fmt"
    "syscall"
)

func main() {
    dll := syscall.MustLoadDLL("kernel32.dll")
    p := dll.MustFindProc("GetVersion")
    v, _, _ := p.Call()
    fmt.Printf("Windows version %d.%d (Build %d)\n", byte(v), uint8(v>>8), uint16(v>>16))
}

If I am running on Linux, I would look inside /proc/version file, or, perhaps, run "uname -a" program and parse its output.

Alex

Hotei

unread,
Mar 19, 2012, 8:49:19 PM3/19/12
to golan...@googlegroups.com, Hotei
If you have the ability to set GOPATH, use the same method to set WINVER="Win7" (for windows 7) or WINVER="XP" or whatever is appropriate.   Then you can inspect it in your go program with Version := os.Getenv("WINVER")  and then

 if  Version == "Win7" {... } 

It's a pain to set up if you have a lot of users, but it will definitely work.  You can put any OS info you need in there, as long as you use unique ENV variable names you should have no problems.

Graham Anderson

unread,
Mar 20, 2012, 4:58:48 AM3/20/12
to golan...@googlegroups.com

Alternatives for these might be:

Windows: systeminfo | findstr /C:"OS"

Should print some additional info in addition to Alex' dll call.

Linux: lsb_release -a

Should print Distro name, distro version, arch, and baseline capabilities as
per LSB (but not kernel version as per uname -a)

Amitabh Arya

unread,
Mar 20, 2012, 8:28:36 AM3/20/12
to Graham Anderson, golan...@googlegroups.com
Yeah, but for systemInfo and Fll call. Application should know the platform, windows or Linux. As dll and systemInfo both does not work on Linux.

And the question is, how can we get the os platform.
--

Thanks,
Amitabh Arya

Benny Siegert

unread,
Mar 20, 2012, 10:44:22 AM3/20/12
to golang-nuts
On Tue, Mar 20, 2012 at 13:28, Amitabh Arya <amitab...@gmail.com> wrote:
> Yeah, but for systemInfo and Fll call. Application should know the platform,
> windows or Linux. As dll and systemInfo both does not work on Linux.
>
> And the question is, how can we get the os platform.

switch runtime.GOOS {
case "windows": // ...
case "linux": // ...
case "freebsd": // ...
}

--Benny.


--
The first essential in chemistry is that you should perform practical
work and conduct experiments, for he who performs not practical work
nor makes experiments will never attain the least degree of mastery.
        -- Abu Musa Jabir ibn Hayyan (721-815)

Graham Anderson

unread,
Mar 20, 2012, 2:15:05 PM3/20/12
to Amitabh Arya, golan...@googlegroups.com
On Tuesday 20 Mar 2012 08:28:36 Amitabh Arya wrote:
> And the question is, how can we get the os platform.

The binary will either be PE, ELF or Mach format.

Reply all
Reply to author
Forward
0 new messages