Programming-Idioms

History of Idiom 162 > diff from v1 to v2

Edit summary for version 2 :
New Go implementation by user [programming-idioms.org]

Version 1

2017-07-30, 19:06:03

Version 2

2017-08-02, 21:41:18

Idiom #162 Execute procedures depending on options

execute bat if b is a program option and fox if f is a program option.

Idiom #162 Execute procedures depending on options

execute bat if b is a program option and fox if f is a program option.

Imports
import "flag"
Code
var b = flag.Bool("b", false, "Do bat")
var f = flag.Bool("f", false, "Do fox")

func main() {
	flag.Parse()
	if *b {
		bar()
	}
	if *f {
		fox()
	}
}
Comments bubble
It is idiomatic to use package flag, and pass options as -b -f
(with a dash).
Doc URL
https://golang.org/pkg/flag/#Bool
Demo URL
https://play.golang.org/p/cvK2kH9SKn