brainman
unread,Feb 23, 2012, 7:49:31 PM2/23/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golan...@googlegroups.com
C:\a\code\src\a>dir
Volume in drive C has no label.
Directory of C:\a\code\src\a
24/02/2012 11:42 AM <DIR> .
24/02/2012 11:42 AM <DIR> ..
24/02/2012 11:38 AM 9 a.bat
24/02/2012 11:42 AM 669 test.go
2 File(s) 678 bytes
2 Dir(s) 130,362,322,944 bytes free
C:\a\code\src\a>type a.bat
exit %1
C:\a\code\src\a>type test.go
package main
import (
"os/exec"
"fmt"
"syscall"
)
// run runs arg command. It retrns error if command could not be run.
// If command did run, the function will return error == nil and
// integer command exit code.
func run(arg ...string) (int, error) {
err := exec.Command(arg[0], arg[1:]...).Run()
if err != nil {
if e2, ok := err.(*exec.ExitError); ok {
if s, ok := e2.Sys().(syscall.WaitStatus); ok {
return int(s.ExitCode), nil
}
}
return 0, err
}
return 0, nil
}
func try(argv ...string) {
rc, err := run(argv...)
fmt.Printf("rc=%d err=%v\n", rc, err)
}
func main() {
try(`.\a.bat`, "0")
try(`.\a.bat`, "1")
try(`.\b.bat`, "0")
}
C:\a\code\src\a>go run test.go
rc=0 err=<nil>
rc=1 err=<nil>
rc=0 err=exec: ".\\b.bat": GetFileAttributesEx .\b.bat: The system cannot find the file specified.
C:\a\code\src\a>go version
go version weekly.2012-02-14 +2aaa29fe8c5f
C:\a\code\src\a>