Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d7db230

Browse files
committed
Add basic usage of flag package
1 parent 9ec9ed3 commit d7db230

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎flag/main.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
)
7+
8+
var (
9+
// Return a point which can read by *name after flag.Parse() executed
10+
name = flag.String("name", "unknow", "your name")
11+
)
12+
13+
func main() {
14+
flag.Parse()
15+
16+
// Create a visitor function, execute with a *flag.Flag callback param for each flag
17+
visitor := func(f *flag.Flag) {
18+
fmt.Println("option =", f.Name, " value =", f.Value)
19+
}
20+
flag.VisitAll(visitor)
21+
22+
fmt.Println(*name)
23+
}
24+
25+
// cmd : go run main.go -name tmpbook
26+
// output:
27+
// option = name value = tmpbook
28+
// tmpbook
29+
30+
// cmd : go run main.go
31+
// output:
32+
// option = name value = unknow
33+
// unknow

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /