Home > Software design >  Golang when i try to execute the command there is no output
Golang when i try to execute the command there is no output

Time:12-31

I try to execute some commands (inside config.json) Only npm commands have display output

This is my first time using Stackoverflow Please forgive me if I did not do well

expected result: when i write any command in config.json will output the correct result

main.go

func main () {
    file, _ := os.Open("config.json")
    byteres, _ := ioutil.ReadAll(file)
    var config Config
    json.Unmarshal(byteres, &config)
    defer file.Close()

    process := exec.Command(config.StartCommand)
    stdout, _ := process.StdoutPipe()
    processscanner := bufio.NewScanner(stdout)
    processscanner.Split(bufio.ScanLines)

    process.Start()
    go func() {
        for processscanner.Scan() {
            message := processscanner.Text()
            fmt.Println(message)
        }
    }()
}

config.json

{
    "StartCommand": "npm"
}

CodePudding user response:

Your main function isn't waiting for your goroutine to finish. Use sync.WaitGroup to block main until the goroutine is done.

  •  Tags:  
  • go
  • Related