英文:
Golang when i try to execute the command there is no output
问题
我尝试执行一些命令(在config.json中)。
只有npm命令会显示输出。
这是我第一次使用Stackoverflow,请原谅我如果做得不好。
期望的结果是:
当我在config.json中写入任何命令时,
将输出正确的结果。
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"
}
英文:
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"
}
答案1
得分: 0
你的主函数没有等待 goroutine 完成。使用 sync.WaitGroup 来阻塞 main
直到 goroutine 完成。
英文:
Your main function isn't waiting for your goroutine to finish. Use sync.WaitGroup to block main
until the goroutine is done.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论