Exit Challenge in Go

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

The Exit challenge is designed to test your ability to use the os.Exit function in Go to immediately exit with a given status.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/NetworkingGroup(["`Networking`"]) go/NetworkingGroup -.-> go/exit("`Exit`") subgraph Lab Skills go/exit -.-> lab-15394{{"`Exit Challenge in Go`"}} end

Exit

The problem to be solved in this challenge is to exit a Go program with a specific status code using the os.Exit function.

Requirements

To complete this challenge, you will need to have a basic understanding of Go programming and the os package.

Example

##  If you run `exit.go` using `go run`, the exit
## will be picked up by `go` and printed.
$ go run exit.go
exit status 3

## By building and executing a binary you can see
## the status in the terminal.
$ go build exit.go
$ ./exit
$ echo $?
3

## Note that the `!` from our program never got printed.

Summary

In this challenge, you learned how to use the os.Exit function to immediately exit a Go program with a specific status code. Remember that unlike other programming languages, Go does not use an integer return value from main to indicate exit status.

Other Go Tutorials you may like