Getting uname-like information from OS

1,438 views
Skip to first unread message

Boris Solovyov

unread,
Jan 10, 2013, 8:57:30 AM1/10/13
to golang-nuts
Hello list,

A quick one, hopefully: does Go library provide a way to get information like `uname` command returns? Kernel name, kernel version, etc? I can't see it (except for os.Hostname()) so I think I should probably exec uname.

Thanks
Boris

minux

unread,
Jan 10, 2013, 9:06:16 AM1/10/13
to Boris Solovyov, golang-nuts
On Thu, Jan 10, 2013 at 9:57 PM, Boris Solovyov <boris.s...@gmail.com> wrote:
A quick one, hopefully: does Go library provide a way to get information like `uname` command returns? Kernel name, kernel version, etc? I can't see it (except for os.Hostname()) so I think I should probably exec uname.
perhaps you can use syscall.Uname to achieve your goal on Unix. 

Boris Solovyov

unread,
Jan 10, 2013, 9:09:34 AM1/10/13
to golang-nuts
Yes, thank you. I missed that somehow. I'll worry about Windows later.

Jan Mercl

unread,
Jan 10, 2013, 9:09:13 AM1/10/13
to minux, Boris Solovyov, golang-nuts
On Thu, Jan 10, 2013 at 3:06 PM, minux <minu...@gmail.com> wrote:
> perhaps you can use syscall.Uname to achieve your goal on Unix.

... or parse `/proc/something`. Example:
https://github.com/cznic/fileutil/blob/master/fileutil_linux.go#L27

-j

Boris Solovyov

unread,
Jan 10, 2013, 9:18:31 AM1/10/13
to golang-nuts
Yes, I may have to do that, or exec a program like uname. I didn't realize that syscall.Uname and syscall.Utsname wasn't defined on all OS'es and so my program won't compile on OS'es where it doesn't exist, like Mac for example. I guess there is no #ifdef in Go, so how does one work around this kind of problems to make a program that can compile with features that exist somewhere but not other places?

minux

unread,
Jan 10, 2013, 9:22:35 AM1/10/13
to Boris Solovyov, golang-nuts
On Thu, Jan 10, 2013 at 10:18 PM, Boris Solovyov <boris.s...@gmail.com> wrote:
On Thu, Jan 10, 2013 at 9:09 AM, Jan Mercl <0xj...@gmail.com> wrote:
On Thu, Jan 10, 2013 at 3:06 PM, minux <minu...@gmail.com> wrote:
> perhaps you can use syscall.Uname to achieve your goal on Unix.... or parse `/proc/something`. Example:
https://github.com/cznic/fileutil/blob/master/fileutil_linux.go#L27
Yes, I may have to do that, or exec a program like uname. I didn't realize that syscall.Uname and syscall.Utsname wasn't defined on all OS'es and so my program won't compile on OS'es where it doesn't exist, like Mac for example. I guess there is no #ifdef in Go, so how does one work around this kind of problems to make a program that can compile with features that exist somewhere but not other places?
use build tags.

or name your file like this:
filename_GOOS.go
where GOOS is the os platform it's intended (for example, linux, freebsd, darwin, windows)


Aram Hăvărneanu

unread,
Jan 10, 2013, 9:23:40 AM1/10/13
to Boris Solovyov, golang-nuts

Boris Solovyov

unread,
Jan 10, 2013, 9:28:25 AM1/10/13
to golang-nuts
On Thu, Jan 10, 2013 at 9:23 AM, Aram Hăvărneanu <ara...@mgk.ro> wrote:
http://golang.org/pkg/go/build/#hdr-Build_Constraints

OK, thanks! I did not know about this, obviously. Seems very simple and Go-like. 

Reply all
Reply to author
Forward
0 new messages