Golang operating system: Difference between revisions

From wikinotes
(Created page with " = Host Info = <blockquote> <syntaxhighlight lang="go"> require "os" host, err := os.Hostname() </syntaxhighlight> </blockquote><!-- Host Info --> = User Info = <blockquote> User info is exposed by a struct of passwd info. <syntaxhighlight lang="go"> userinfo, err := user.Lookup("will") if errors.Is(err, user.UnknownUserError) {...} fmt.Println(userinfo.Uid) </syntaxhighlight> </blockquote><!-- User Info -->")
 
Line 3: Line 3:
<blockquote>
<blockquote>
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
require "os"
import "os"


host, err := os.Hostname()
host, err := os.Hostname()
</syntaxhighlight>
runtime exposes uname-like info
<syntaxhighlight lang="go">
import "runtime"
runtime.GOOS    // platform (ex. 'linux')
runtime.GOARCH  // executable's target cpu arch (ex. 'amd64')
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Host Info -->
</blockquote><!-- Host Info -->

Revision as of 13:18, 18 June 2022

Host Info

import "os"

host, err := os.Hostname()

runtime exposes uname-like info

import "runtime"

runtime.GOOS    // platform (ex. 'linux')
runtime.GOARCH  // executable's target cpu arch (ex. 'amd64')

User Info

User info is exposed by a struct of passwd info.

userinfo, err := user.Lookup("will")

if errors.Is(err, user.UnknownUserError) {...}
fmt.Println(userinfo.Uid)