Learning go, trying to figure out the exec package. In what ways can I improve my code? -


wrote simple program calls "ls", passes each line through regexp filtering files end in "s". ls used purposes of learning exec package. how can improve code below more correct/succinct/go-ish?

package main  import (     "bufio"     "fmt"     "os/exec"     "regexp" )  func main() {      cmd := exec.command("ls")     stdout, _ := cmd.stdoutpipe()     s := bufio.newreader(stdout)     cmd.start()      go cmd.wait()      {         l, _, err := s.readline()         if err != nil {             break         }          if m, err := regexp.match(".*s$", l); m && err == nil {             fmt.println(string(l))         }     } } 

the cmd.output example in standard documentation pretty succinct. doesn't text processing, shows how execute command , output single function call.

here's way combine example yours,

package main  import (     "bytes"     "fmt"     "log"     "os/exec"     "regexp" )  func main() {     out, err := exec.command("ls").output()     if err != nil {         log.fatal(err)     }     fmt.print(string(bytes.join(regexp.mustcompile(".*s\n").findall(out, -1), nil))) } 

if goal broad overview of package, experiment number of functions , methods learn different capabilities. experiment command arguments. experiment different fields of cmd struct. try not distracted other packages regexp, simplest examples exercise package feature.

of course if see how might use exec feature in real application, try it. you'll learn 1 feature in more depth.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -