Created
August 12, 2023 21:09
-
-
Save zianwar/fb1e880acd5c1aa891e5267ad3fd3365 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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