Created
May 19, 2016 20:34
-
-
Save nqn/3ab97ad4a414ea7de3595e81f93c895d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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