How on golang find out the CPU load, computer name and user name on the computer?

  • os / user will answer at least two recent questions. - KoVadim

1 answer 1

On stackoverflow, it is customary not to mix many questions into one.

1. Current user name

Package os/user . Example:

 package main import ( "os/user" "log" "fmt" ) func main() { // текущий пользователь cur, err := user.Current() if err != nil { log.Fatal(err) } fmt.Println("Current user:", cur) } 

There is another choice of the user by name and by identifier. Play.golang.org does not work.

2. Computer name.

The os.Hostname function. Play

3. Processor load.

Use third-party libraries.

  • @ "Ivan Black" Thank you. And which third-party libraries and how to connect them? Could you tell? - Rakzin Roman pm
  • I just here found socketloop.com/references/… - Rakzin Roman
  • @RakzinRoman, I used this one gosigar . But github not a stable branch. However, you can use the vendor folder if go1.5 +. Or godep for older versions. - Ivan Black
  • @RakzinRoman, here is an example of using from topbeat . But, I needed it for Linux - Ivan Black