go - How do you print multiline exec output in golang -


i'm trying write simple golang program lists files in directory. whenever shell command yields multiple lines, registers in go array

for example, when try following:

import (     "log"     "os/exec"     "fmt" ) func main (){      out,err := exec.command("ls").output()     if err != nil {         log.fatal(err)     }     fmt.println(out)  } 

i end output [101 108 105 109 115 116 97 116 115 46 105 109 108 10 101 110 118 10 115 99 114 97 116 99 104 10 115 114 99 10]

i feel common thing wasn't able find on here anywhere.

the return type of first value output []byte. fmt.println displaying numeric values of each slice element.

to show desired result of output of command, can either convert byte slice string or use format string %s verb:

fmt.println(string(out)) 

or:

fmt.printf("%s\n", out) 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -