Skip to content

Instantly share code, notes, and snippets.

@kjk
Last active Nov 5, 2019
Embed
What would you like to do?
command line arguments
// :run go run main.go -echo echo-arg additional arg
// :collection Essential Go
package main
import (
"flag"
"fmt"
"os"
)
// :show start
var (
flgHelp bool
flgEcho string
)
func parseCmdLineFlags() {
flag.BoolVar(&flgHelp, "help", false, "if true, show help")
flag.StringVar(&flgEcho, "echo", "", "")
flag.Parse()
}
func main() {
parseCmdLineFlags()
if flgHelp {
flag.Usage()
os.Exit(0)
}
fmt.Printf("flag -echo: '%s'\n", flgEcho)
remainingArgs := flag.Args()
for _, arg := range remainingArgs {
fmt.Printf("Remainig arg: '%s'\n", arg)
}
}
// :show end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment