os.Exit doesn't exit the program with the specified error code as expected

272 views
Skip to first unread message

Maisa Unbelievable

unread,
Sep 23, 2024, 9:42:31 AM9/23/24
to golang-nuts
Hello there! I don't quiet understand why os.Exit doesn't terminate my
program according to its documentation:

Exit causes the current program to exit with the given status code.

My program implements a rudimentary CLI parsing:

package main

import (
"fmt"
"os"
)

func main() {
var name string
var age string

args := os.Args[1:]
i := 0

for i+1 < len(args) {
option := args[i]
value := args[i+1]

switch option {
case "--name":
name = value
case "--age":
age = value
default:
fmt.Printf("Unknown option %s", option)
os.Exit(1) // <-- I expected this function to terminate the program
}

i += 2
}

fmt.Printf("name == %s, age == %s", name, age)
}


Basically, I wanna rewrite this Fish program to Go:

#!/usr/bin/env fish

set --local name
set --local age

set --local i 1

while set --query argv[$i]
    set --local option "$argv[$i]"
    set --local value "$argv[$(math $i + 1)]"

    switch "$option"
        case --name
            set name $value
        case --age
            set age $value
        case '*'
            printf "Unknown option %s" $option
            return 1 # I expected os.Exit to be a "return {{code}}" shell equivalent
    end

    set i (math $i + 2)
end

printf "name == %s, age == %s" $name $age

For the context: I'm new to go but have shell scripting experience.

Robert Engels

unread,
Sep 23, 2024, 9:49:02 AM9/23/24
to Maisa Unbelievable, golang-nuts
Are you seeing the “unknown option” being output? 

On Sep 23, 2024, at 8:43 AM, Maisa Unbelievable <emilygrace...@gmail.com> wrote:

Hello there! I don't quiet understand why os.Exit doesn't terminate my
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/03655054-4de5-43c2-9c05-72971c7a961cn%40googlegroups.com.

Brian Candler

unread,
Sep 23, 2024, 10:12:10 AM9/23/24
to golang-nuts
What makes you think that the line which calls os.Exit(1) is being reached?

What are your test arguments to the program?

Diego Joss

unread,
Sep 23, 2024, 10:38:07 AM9/23/24
to Maisa Unbelievable, golang-nuts
Hi,

your for loop condition skips odd numbered argument counts. Consider:
  • args = ["--bad"]
  • len(args) = 1
  • for i+1 < len(args)   -->  condition is false (1 not less than 1)
Thus the only way to trigger the os.Exit(1) is to have at least 2 arguments, of which the first is not "--name" or "--age". For example: args= ["--bad", "anything"]
However if the bad argument is the last, then the for loop exits early.

-Diego

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/03655054-4de5-43c2-9c05-72971c7a961cn%40googlegroups.com.


--
Diego Joss
Electrical Engineer
SamanTree Medical SA
Avenue de Provence 12, 1007 Lausanne, Switzerland
Tel: +41 21 625 08 97

Maisa Unbelievable

unread,
Sep 23, 2024, 10:38:22 AM9/23/24
to golang-nuts
> Are you seeing the “unknown option” being output?

Yes, but I didn't see that the program stopped right after it. I am not sure why... Maybe I've misinterpreted something (I don't exclude this possibility, as I'm new to Go).


> What are your test arguments to the program?

go run test.go --name a --unknown 35

I don't get why when I write os.Exit(25) my exit status is set to 1 inside a shell instead of 25:

package main

import (
"fmt"
"os"
)

func main() {
fmt.Println("Execution started...")
os.Exit(25)
}

terminal.png

And why I have exit status 25 line printed to stderr?.. I mean, it's not a problem to 2> /dev/null it but anyway, why?

BTW, is this forum suitable for newbie in Go questions, or should I ask them somewhere else?

Robert Engels

unread,
Sep 23, 2024, 10:42:26 AM9/23/24
to Maisa Unbelievable, golang-nuts
In this case you are getting the status of ‘go run’. You need to compile and run the binary to get the correct status. 

On Sep 23, 2024, at 9:38 AM, Maisa Unbelievable <emilygrace...@gmail.com> wrote:

> Are you seeing the “unknown option” being output?

Yes, but I didn't see that the program stopped right after it. I am not sure why... Maybe I've misinterpreted something (I don't exclude this possibility, as I'm new to Go).

> What are your test arguments to the program?

go run test.go --name a --unknown 35

I don't get why when I write os.Exit(25) my exit status is set to 1 inside a shell instead of 25:

package main

import (
"fmt"
"os"
)

func main() {
fmt.Println("Execution started...")
os.Exit(25)
}

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages