Skip to content

Instantly share code, notes, and snippets.

@nqn
Created May 19, 2016 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nqn/3ab97ad4a414ea7de3595e81f93c895d to your computer and use it in GitHub Desktop.
Save nqn/3ab97ad4a414ea7de3595e81f93c895d to your computer and use it in GitHub Desktop.
package main
import (
"github.com/pivotal-golang/bytefmt"
"strconv"
"fmt"
)
type CPUInfo struct {
sockets int
physicalCores int
threadsPerCore int
cacheL1i int
cacheL1d int
cacheL2 int
cacheL3 int
}
func main() {
info := CPUInfo{}
info.Parse()
fmt.Printf("%v\n", info)
}
func (cputopo *CPUInfo) Parse() error {
parseMap := map[string]*int{
"Socket(s)": &cputopo.sockets,
"Core(s) per socket", &cputopo.physicalCores,
}
key := "Socket(s)"
value := "5"
ptr, ok := parseMap[key]
if ok {
bytes, err := bytefmt.ToBytes(value)
if err == nil {
*ptr = int(bytes)
return nil
}
t, err := strconv.Atoi(value)
if err != nil {
return err
}
*ptr = t
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment