Skip to content

Instantly share code, notes, and snippets.

@kjk
Last active November 5, 2019 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjk/31b5364c35801027a2929b17e9155425 to your computer and use it in GitHub Desktop.
Save kjk/31b5364c35801027a2929b17e9155425 to your computer and use it in GitHub Desktop.
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