Getting Python Version Using Go

I am trying to get the Python version using Go:

import ( "log" "os/exec" "strings" ) func verifyPythonVersion() { _, err := exec.LookPath("python") if err != nil { log.Fatalf("No python version located") } out, err := exec.Command("python", "--version").Output() log.Print(out) if err != nil { log.Fatalf("Error checking Python version with the 'python' command: %v", err) } fields := strings.Fields(string(out)) log.Print(fields) } func main() { verifyPythonVersion() } 

Returns empty fragments:

 2014/01/03 20:39:53 [] 2014/01/03 20:39:53 [] 

Any idea what I'm doing wrong?

+6
source share
1 answer
 $ python --version Python 2.7.2 $ python --version 1>/dev/null # hide stdout Python 2.7.2 $ python --version 2>/dev/null # hide stderr 

We can conclude that the output goes to stderr. Now I looked at Go docs and guessed that cmd.Output only fixes stdout ( docs ). You should use cmd.CombinedOutput ( docs ):

CombinedOutput runs the command and returns its combined standard output and standard error.

+8
source

All Articles