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 -->")
 
 
(2 intermediate revisions by the same user not shown)
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 -->


= User Info =
= Users, Groups =
<blockquote>
<blockquote>
User info is exposed by a struct of passwd info.
User info is exposed by a struct of passwd info.


<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
userinfo, err := user.Lookup("will")
import "os/user"


usr, err := user.Lookup("will")
if errors.Is(err, user.UnknownUserError) {...}
if errors.Is(err, user.UnknownUserError) {...}
fmt.Println(userinfo.Uid)
fmt.Println(usr.Uid)
 
user.Current()  // 'will'
</syntaxhighlight>
 
<syntaxhighlight lang="go">
import "os/user"
 
grp, err := user.LookupGroup("audio")
if errors.Is(err, user.UnknownGroupError) {...}
fmt.Println(grp.Gid)
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- User Info -->
</blockquote><!-- Users, Groups -->

Latest revision as of 13:27, 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')

Users, Groups

User info is exposed by a struct of passwd info.

import "os/user"

usr, err := user.Lookup("will")
if errors.Is(err, user.UnknownUserError) {...}
fmt.Println(usr.Uid)

user.Current()  // 'will'
import "os/user"

grp, err := user.LookupGroup("audio")
if errors.Is(err, user.UnknownGroupError) {...}
fmt.Println(grp.Gid)