How to print exit status in golang?

Hi Friends šŸ‘‹,

Welcome To aGuideHub! ā¤ļø

Today, Iā€™m going to show you how to print exit status in golang, well in this tutorial we will use the log.Printf() method to print exit status.

luckily, i have found some exit status code example, here we have to focus log.Printf("Exit Status: %d", status.ExitStatus()) line.

package main

import "os/exec"
import "log"
import "syscall"

func main() {
    cmd := exec.Command("git", "blub")

    if err := cmd.Start(); err != nil {
        log.Fatalf("cmd.Start: %v", err)
    }

    if err := cmd.Wait(); err != nil {
        if exiterr, ok := err.(*exec.ExitError); ok {
            if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
                log.Printf("Exit Status: %d", status.ExitStatus())
            }
        } else {
            log.Fatalf("cmd.Wait: %v", err)
        }
    }
}

When we run the above code in the golang compiler, we get an error plus an Exit Status log.

Check the output of the above code example.

print, exit status, golang

Try it yourself

All the best šŸ‘

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts