0xBF
0xBF

0xBF

Go cheatsheet - Flag

0xBF's photo
0xBF

Published on Apr 20, 2021

1 min read

Flag

package main

import (
    "flag"
    "fmt"
)

func main() {
    count := flag.Int("count", 0, "run time count")
    prefix := flag.String("prefix", "", "install path")
    dryrun := flag.Bool("dryrun", true, "dry run program")

    flag.Parse()

    fmt.Println("Count", *count)
    fmt.Println("Prefix", *prefix)
    fmt.Println("Dryrun", *dryrun)
}

Pass to command line:

$ ./main --count=2 --prefix=/path/to/folder --dryrun=false
$ ./main -count=2 -prefix=/path/to/folder -dryrun=false
$ ./main -count 2 -prefix /path/to/folder -dryrun false

Also the -h and --help is auto generated

$ ./main -h
$ ./main --help
 
Share this