title: Go build & exec: fork/exec: permission denied
body: <p>I need to build a program using the Go toolchain and then execute it. For some reasons I get a permission error due the forking. Is there a way to circumvent this error or any best practice? I think my program does something similar with <code>Go test</code> tool, though <code>go test</code> doesn't get this kind of error.</p>
  
  <pre><code>package main
  
  import(
      "os"
      "os/exec"
      "flag"
      log "github.com/golang/glog"
  )
  
  func main(){
      flag.Parse()
      tdir := "abc"
      if err := os.MkdirAll(tdir, 0777); err !=nil{
          log.Error(err)
          return
      }
      f, err := os.Create(tdir + "/main.go")
      if err !=nil{
          log.Error(err)
          return
      }
      if err = f.Chmod(0777); err !=nil{
          log.Error(err)
          return
      }
      defer f.Close()
      defer os.Remove(f.Name())
      if _, err = f.Write([]byte(tpl)); err !=nil{
          log.Error(err)
          return
      }
      cmd := exec.Command("go", "build", "-o", "edoc")
      cmd.Path = tdir
      b, err := cmd.CombinedOutput()
      if err !=nil{
          log.Errorf("%s, err %v", b, err)
          return
      }
  
  }
  
  var tpl = `package main
  
  import(
      "fmt"
      "flag"
  )
  
  func main(){
      flag.Parse()
      fmt.Printf("Hello World")
  
  }`
  </code></pre>
  
  <p>Error: </p>
  
  <pre><code>E0202 18:24:42.359008   13600 main.go:36] , err fork/exec abc: permission denied
  </code></pre>
  
  <p>OS: OSX 10.11</p>