exit snippets

How read process exit code in Golang

Tagged code, exit, status  Languages go

This function returns true if the nz command returns exit code 0:

package main

import (
    "os/exec"
    "strconv"
)

// IsPortOpen returns a boolean that indicates if a port is open or closed.
func IsPortOpen(host string, port int) bool {
    args := []string{"-z", host, strconv.Itoa(port), "-G", "5"}
    cmd := exec.Command("/usr/bin/nc", args...)
    _, err := cmd.Output()
    if werr, ok := err.(*exec.ExitError); ok {
        if s := werr.Error(); s != "0" {
            return false
        }
    }
    return true
}