Exit code in Windows

1,044 views
Skip to first unread message

Archos

unread,
Feb 23, 2012, 8:29:42 AM2/23/12
to golang-nuts
How to get the exit code into a windows system?
* Because (I think) the package syscall is for Unix systems


(*ProcessState).Sys().(syscall.WaitStatus).ExitStatus()

http://weekly.golang.org/pkg/os/#ProcessState.Sys
http://weekly.golang.org/pkg/syscall/#WaitStatus.ExitStatus

peterGo

unread,
Feb 23, 2012, 9:52:05 AM2/23/12
to golang-nuts
Archos,

On the contrary, the Go syscall package is where the operating system
specific code resides. Look in $GOROOT/src/pkg/syscall. You will see
the source code is labelled by OS.

If you don't have access to a Windows system, post a small piece of
working Linux code, which we can then run and test under Windows.

Peter

Archos

unread,
Feb 23, 2012, 10:00:09 AM2/23/12
to golang-nuts
Yes, they're installed

$ ll $GOROOT/src/pkg/syscall/*window*go

The confusion has been because there're only files related to Unix in
section "Package files", in syscall's documentation:

http://weekly.golang.org/pkg/syscall/
> >http://weekly.golang.org/pkg/os/#ProcessState.Syshttp://weekly.golang...

Archos

unread,
Feb 23, 2012, 10:05:32 AM2/23/12
to golang-nuts


On Feb 23, 3:00 pm, Archos <raul....@sent.com> wrote:
> Yes, they're installed
>
>     $ ll $GOROOT/src/pkg/syscall/*window*go
>
> The confusion has been because there're only files related to Unix in
> section "Package files", in syscall's documentation:
>
because the documentation web is installed over an Unix system

brainman

unread,
Feb 23, 2012, 7:49:31 PM2/23/12
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>

minux

unread,
Feb 24, 2012, 12:45:40 PM2/24/12
to brainman, golan...@googlegroups.com
Hi Alex,
    Do you think it's necessary to write an wiki article for this problem on go-wiki?
I'd like to add an entry using your code, if you permit.

brainman

unread,
Feb 24, 2012, 3:26:38 PM2/24/12
to golan...@googlegroups.com
minux,

Feel free to use it any way you like.

Alex

Reply all
Reply to author
Forward
0 new messages