forked from astaxie/gopkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
506 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
## func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) | ||
|
||
参数列表 | ||
- p *time.Duration 需要与flag值绑定的变量的指针 | ||
- name string flag名称 | ||
- value time.Duration 默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
|
||
功能说明 | ||
- 将一个time.Duration类型的flag值绑定到一个time.Duration的变量 | ||
|
||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
const ( | ||
defaultUsage = "help message" | ||
) | ||
|
||
var ( | ||
myFlagSet = flag.NewFlagSet("newflagset", flag.ExitOnError) | ||
duratFlag = myFlagSet.Duration("duratFlag", 20, defaultUsage) | ||
duratVar time.Duration | ||
) | ||
|
||
func init() { | ||
myFlagSet.DurationVar(&duratVar, "duratVar", 100, defaultUsage) | ||
} | ||
|
||
func main() { | ||
args := []string{ | ||
"-duratVar", "2m", | ||
"-duratFlag", "12h", | ||
"arg2", | ||
} | ||
myFlagSet.Parse(args) | ||
fmt.Println("duratFlag", *duratFlag) | ||
fmt.Println("duratVar", duratVar) | ||
} | ||
|
||
代码输出 | ||
|
||
// ./testflagsetduration | ||
duratFlag 12h0m0s | ||
duratVar 2m0s | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
## func (f *FlagSet) Float64(name string, value float64, usage string) *float64 | ||
|
||
|
||
参数列表 | ||
|
||
- name string flag名称 | ||
- value float64 默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
- *float64 返回一个float64类型的flag值的地址 | ||
|
||
功能说明 | ||
- 为flag集合f增加一个带默认值和语句的float64类型的flag,返回flag对应值的地址 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
const ( | ||
defaultUsage = "help message" | ||
) | ||
|
||
var ( | ||
myFlagSet = flag.NewFlagSet("newflagset", flag.ExitOnError) | ||
float64Flag = myFlagSet.Float64("float64Flag", 0.9917, defaultUsage) | ||
float64Var float64 | ||
) | ||
|
||
func init() { | ||
myFlagSet.Float64Var(&float64Var, "float64Var", 0.102, defaultUsage) | ||
} | ||
|
||
func main() { | ||
args := []string{ | ||
"-float64Flag", "2718e28", | ||
"-float64Var", "-12.78", | ||
"arg2", | ||
} | ||
myFlagSet.Parse(args) | ||
fmt.Println("float64Flag", *float64Flag) | ||
fmt.Println("float64Var", float64Var) | ||
} | ||
|
||
代码输出 | ||
|
||
./testflagsetfloat64 | ||
float64Flag 2.718e+31 | ||
float64Var -12.78 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
## func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string) | ||
|
||
参数列表 | ||
- p *float64 需要与flag参数值绑定的变量地址 | ||
- name string flag名称 | ||
- value float64 变量默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
|
||
功能说明 | ||
- 将f中指定flag参数值绑定到float64变量 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
const ( | ||
defaultUsage = "help message" | ||
) | ||
|
||
var ( | ||
myFlagSet = flag.NewFlagSet("newflagset", flag.ExitOnError) | ||
float64Flag = myFlagSet.Float64("float64Flag", 0.9917, defaultUsage) | ||
float64Var float64 | ||
) | ||
|
||
func init() { | ||
myFlagSet.Float64Var(&float64Var, "float64Var", 0.102, defaultUsage) | ||
} | ||
|
||
func main() { | ||
args := []string{ | ||
"-float64Flag", "2718e28", | ||
"-float64Var", "-12.78", | ||
"arg2", | ||
} | ||
myFlagSet.Parse(args) | ||
fmt.Println("float64Flag", *float64Flag) | ||
fmt.Println("float64Var", float64Var) | ||
} | ||
|
||
代码输出 | ||
|
||
./testflagsetfloat64 | ||
float64Flag 2.718e+31 | ||
float64Var -12.78 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
## func (f *FlagSet) Init(name string, errorHandling ErrorHandling) | ||
|
||
参数列表 | ||
- name string 名称 | ||
- errorHandling ErrorHandling 错误处理方式,包括`ContinueOnError`:出错仍继续, `ExitOnError`:出错后退出程序,`PanicOnError`:出错后panic三种错误处理方式 | ||
|
||
返回值 | ||
|
||
功能说明 | ||
- 设置flag集合的名称和错误处理方式 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var myFlagSet = flag.NewFlagSet("testFlagSet", flag.ExitOnError) | ||
|
||
func main() { | ||
fmt.Println("Before Set:", myFlagSet) | ||
myFlagSet.Init("myFlagSet", flag.ContinueOnError) | ||
fmt.Println("After Set:", myFlagSet) | ||
} | ||
|
||
代码输出 | ||
|
||
// ./testflagsetinit | ||
Before Set: &{<nil> testFlagSet false map[] map[] [] 1 ?reflect.Value?} | ||
After Set: &{<nil> myFlagSet false map[] map[] [] 0 ?reflect.Value?} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
## func (f *FlagSet) Int(name string, value int, usage string) *int | ||
|
||
参数列表 | ||
- name string flag名称 | ||
- value int 变量默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
- *int 返回一个int类型的flag值的地址 | ||
|
||
功能说明 | ||
- 为flag集合f增加一个带默认值和提示语句的int类型flag,返回对应值的地址 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
const ( | ||
defaultUsage = "help message" | ||
) | ||
|
||
var ( | ||
myFlagSet = flag.NewFlagSet("newflagset", flag.ExitOnError) | ||
intFlag = myFlagSet.Int("intFlag", 100, defaultUsage) | ||
intVar int | ||
) | ||
|
||
func init() { | ||
myFlagSet.IntVar(&intVar, "intVar", 20, defaultUsage) | ||
} | ||
|
||
func main() { | ||
args := []string{ | ||
"--intFlag", "22", | ||
"-intVar", "-10", | ||
"arg2", | ||
} | ||
myFlagSet.Parse(args) | ||
fmt.Println("intFlag", *intFlag) | ||
fmt.Println("intVar", intVar) | ||
} | ||
|
||
代码输出 | ||
|
||
// ./testflagsetint | ||
intFlag 22 | ||
intVar -10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
## func (f *FlagSet) IntVar(p *int, name string, value int, usage string) | ||
|
||
参数列表 | ||
- p *int 需要与flag参数值绑定的变量地址 | ||
- name string flag名称 | ||
- value int 变量默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
|
||
功能说明 | ||
- 将f中指定flag参数值绑定到int变量 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
const ( | ||
defaultUsage = "help message" | ||
) | ||
|
||
var ( | ||
myFlagSet = flag.NewFlagSet("newflagset", flag.ExitOnError) | ||
intFlag = myFlagSet.Int("intFlag", 100, defaultUsage) | ||
intVar int | ||
) | ||
|
||
func init() { | ||
myFlagSet.IntVar(&intVar, "intVar", 20, defaultUsage) | ||
} | ||
|
||
func main() { | ||
args := []string{ | ||
"--intFlag", "22", | ||
"-intVar", "-10", | ||
"arg2", | ||
} | ||
myFlagSet.Parse(args) | ||
fmt.Println("intFlag", *intFlag) | ||
fmt.Println("intVar", intVar) | ||
} | ||
|
||
代码输出 | ||
|
||
// ./testflagsetint | ||
intFlag 22 | ||
intVar -10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
## func (f *FlagSet) String(name string, value string, usage string) *string | ||
|
||
参数列表 | ||
- name string flag名称 | ||
- value string 变量默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
- *string 返回一个string类型的flag值的地址 | ||
|
||
功能说明 | ||
- 为f增加一个带默认值和提示语句的string类型flag,返回对应值的地址 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
const ( | ||
defaultUsage = "help message" | ||
) | ||
|
||
var ( | ||
myFlagSet = flag.NewFlagSet("newflagset", flag.ExitOnError) | ||
stringFlag = myFlagSet.String("stringFlag", "defaultValue", defaultUsage) | ||
stringVar string | ||
) | ||
|
||
func init() { | ||
myFlagSet.StringVar(&stringVar, "stringVar", "defaultValue", defaultUsage) | ||
} | ||
|
||
func main() { | ||
args := []string{ | ||
"--stringFlag", "test string flag", | ||
"-stringVar", "test string", | ||
"arg2", | ||
} | ||
myFlagSet.Parse(args) | ||
fmt.Println("stringFlag", *stringFlag) | ||
fmt.Println("stringVar", stringVar) | ||
} | ||
|
||
代码输出 | ||
|
||
// ./testflagsetstring | ||
stringFlag test string flag | ||
stringVar test string |
Oops, something went wrong.