Skip to content

Instantly share code, notes, and snippets.

@zianwar
Created August 12, 2023 21:09
Show Gist options
  • Save zianwar/fb1e880acd5c1aa891e5267ad3fd3365 to your computer and use it in GitHub Desktop.
Save zianwar/fb1e880acd5c1aa891e5267ad3fd3365 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"strings"
)
var (
flDebug bool
flFoo string
)
func init() {
log.SetFlags(0) // Hide timestamps
flag.BoolVar(&flDebug, "debug", false, "run in debug mode")
flag.StringVar(&flFoo, "foo", "", "foo")
}
func parseAndValidateFlags() {
flag.Parse()
if flDebug {
log.Println("Running in debug mode")
}
if strings.TrimSpace(flFoo) == "" {
log.Fatal("Error: 'foo' cannot be empty")
}
}
func main() {
parseAndValidateFlags()
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment