Guest User

Untitled

a guest
Dec 16th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. package main
  2.  
  3. import "os/exec"
  4. import "github.com/golang/glog"
  5. import "fmt"
  6. import "strings"
  7. import "errors"
  8.  
  9. func thisWorks() {
  10. str_out := "hello world n How are you nthis is good"
  11. lines := strings.Split(str_out, "n")
  12. fmt.Printf("lines is n%sn", lines)
  13. }
  14.  
  15. func GetFreeOutput() error {
  16.  
  17. var errMsg string
  18. bytes_out, err := exec.Command("free").Output()
  19.  
  20. // This shows that the output has 10 (newline) in it.
  21. fmt.Println(bytes_out)
  22.  
  23. if err != nil {
  24. errMsg = "Error geting output of free command"
  25. glog.Fatal(errMsg)
  26. return errors.New(errMsg)
  27. }
  28.  
  29. str_out := string(bytes_out)
  30. fmt.Printf("str_out is n%s", str_out)
  31.  
  32. // This is not splitting the lines, it is converting the whole output to a single line.
  33. fmt.Println("nLines are ", strings.Split(str_out, "n"))
  34.  
  35. index_of_newline := strings.Index(str_out, "n")
  36.  
  37. // This gives the index of "n" as 79 on my machine, which is correct.
  38. fmt.Printf("nIndex is %d", index_of_newline)
  39.  
  40. fmt.Println("nLine using index are ", strings.Split(str_out, string(str_out[index_of_newline])))
  41.  
  42. return nil
  43. }
  44.  
  45. func main() {
  46. err := GetFreeOutput()
  47. fmt.Printf("Error is %s", err)
  48. }
Add Comment
Please, Sign In to add comment