In Windows, I can run something like systeminfo | findstr /C:"OS Name
to output the Windows full name to the console. I've tried a couple different variations of piping output from one command to the other, but I just get empty strings.
Example
first := exec.Command("systeminfo")
second := exec.Command("findstr /C:'OS Name'")
reader, writer := io.Pipe()
first.Stdout = writer
second.Stdin = reader
var buffer bytes.Buffer
second.Stdout = &buffer
first.Start()
second.Start()
first.Wait()
writer.Close()
second.Wait()
output := buffer.String()
log.Printf("output: %s", output)
`
Are there any built in methods to get this information?
One way to do it is to use the golang.org/x/sys/windows/registry package.
A simple example:
package main
import (
"fmt"
"golang.org/x/sys/windows/registry"
)
func main() {
key, _ := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE) // error discarded for brevity
defer key.Close()
productName, _, _ := key.GetStringValue("ProductName") // error discarded for brevity
fmt.Println(productName)
}
This prints Windows 8.1 Pro
on my computer.