2017-12-18 00:06 阅读 32


I was reading about exec in Go, and tried to do the same using goroutines.

In the following code, I'm trying to make Go run ls, then print a success message in the main thread. However, it's only printing the ls, but not the success message.

What's going on?


package main

import "syscall"
import "os"
import "os/exec"
import "fmt"

func main() {
    p := fmt.Println
    done := make(chan bool)
    binary, lookErr := exec.LookPath("ls")
    if lookErr != nil {

    args := []string{"ls", "-a", "-l", "-h"}

    env := os.Environ()

    go func() {
        execErr := syscall.Exec(binary, args, env)
        if execErr != nil {
        done <- true


    p("Done with exec")

Here's the output:

Valeriys-MacBook-Pro:test valeriy$ go run test.go 
total 8
drwxr-xr-x  3 valeriy  staff    96B Dec 17 15:46 .
drwxr-xr-x  8 valeriy  staff   256B Dec 17 00:06 ..
-rw-r--r--  1 valeriy  staff   433B Dec 17 15:38 test.go
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dpylt7626401 dpylt7626401 2017-12-18 00:14

    syscall.Exec replaces the current process with the one invoked.

    If you want to run an external command while keeping the original program running, you need to use exec.Command

    By the way, the link you included does say:

    Sometimes we just want to completely replace the current Go process with another (perhaps non-Go) one.

    If you really want to use the syscall package, you can use syscall.StartProcess which does a fork/exec as opposed to a plain exec.

    点赞 评论 复制链接分享