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
16 changed files
with
482 additions
and
5 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 |
---|---|---|
|
@@ -48,6 +48,7 @@ | |
|
||
// ./test_arg abc | ||
abc | ||
|
||
// ./test_arg -flag abc def | ||
def | ||
|
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 |
---|---|---|
|
@@ -48,6 +48,7 @@ | |
|
||
// ./test_args abc def | ||
[abc def] | ||
|
||
// ./test_args -flag abc def ghi | ||
[def ghi] | ||
|
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 |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
- usage string 提示信息 | ||
|
||
返回值 | ||
- bool | ||
|
||
功能说明 | ||
将命令行指定flag值绑定到一个bool变量 | ||
|
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
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
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
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,43 @@ | ||
## func Set(name, value string) error | ||
|
||
参数列表 | ||
- name string | ||
- value string | ||
|
||
返回值 | ||
- error 设置成功返回nil | ||
|
||
功能说明 | ||
- 将名称为name的flag的值设置为value, name必须存在 | ||
|
||
示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var stringFlag = flag.String("test", "test", "help message.") | ||
var intFlag = flag.Int("int", 10, "help message") | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Println("stringFlag:", *stringFlag) | ||
fmt.Println("inFlag", *intFlag) | ||
fmt.Println("After Set.") | ||
flag.Set("test", "set_test") | ||
flag.Set("int", "9999") | ||
fmt.Println("stringFlag:", *stringFlag) | ||
fmt.Println("inFlag:", *intFlag) | ||
} | ||
|
||
代码输出 | ||
|
||
// ./testset -test "before set" -int 1 | ||
stringFlag: before set | ||
inFlag: 1 | ||
After Set. | ||
stringFlag: set_test | ||
inFlag: 9999 |
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,46 @@ | ||
## func String(name string, value string, usage string) *string | ||
|
||
参数列表 | ||
- name string flag名称 | ||
- value string 变量默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
- *string 返回一个string类型的flag值的地址 | ||
|
||
功能说明 | ||
- 定义一个带默认值和提示语句的string类型flag,返回对应值的地址 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var stringFlag = flag.String("string", "default value", "help message for int") | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Println("stringFlag: ", *stringFlag) | ||
} | ||
|
||
|
||
代码输出 | ||
|
||
// ./teststring | ||
stringFlag: default value | ||
|
||
// ./teststring -string | ||
flag needs an argument: -string | ||
Usage of ./teststring: | ||
-string="default value": help message for int | ||
|
||
// ./teststring -string=123 | ||
stringFlag: 123 | ||
|
||
// ./teststring -string 123 | ||
stringFlag: 123 | ||
|
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,48 @@ | ||
## func StringVar(p *string, name string, value string, usage string) | ||
|
||
参数列表 | ||
- p *string 需要与flag参数值绑定的变量地址 | ||
- name string flag名称 | ||
- value string 变量默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
|
||
功能说明 | ||
- 将命令行指定flag参数值绑定到string变量 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var stringFlag string | ||
|
||
func init() { | ||
flag.StringVar(&stringFlag, "flag", "default value", "help message for flag") | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Println("flag: ", stringFlag) | ||
} | ||
|
||
代码输出 | ||
|
||
// ./teststringvar | ||
flag: default value | ||
|
||
// ./teststringvar -flag | ||
flag needs an argument: -flag | ||
Usage of ./teststringvar: | ||
-flag="default value": help message for flag | ||
|
||
// ./teststringvar -flag=12 | ||
flag: 12 | ||
|
||
// ./teststringvar -flag 22 | ||
flag: 22 |
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,45 @@ | ||
## func Uint(name string, value uint, usage string) *uint | ||
|
||
参数列表 | ||
- name string flag名称 | ||
- value uint 变量默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
- *uint 返回一个uint类型的flag值的地址 | ||
|
||
功能说明 | ||
- 定义一个带默认值和提示语句的uint类型flag,返回对应值的地址 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var uintFlag = flag.Uint("uint", 100, "help message for uint") | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Println("uintFlag: ", *uintFlag) | ||
} | ||
|
||
|
||
代码输出 | ||
|
||
// ./testuint | ||
uintFlag: 100 | ||
|
||
// ./testuint -uint 100 | ||
uintFlag: 100 | ||
|
||
// ./testuint -uint=100 | ||
uintFlag: 100 | ||
|
||
// ./testuint -uint | ||
flag needs an argument: -uint | ||
Usage of ./testuint: | ||
-uint=100: help message for uint |
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,44 @@ | ||
## func Uint64(name string, value uint64, usage string) *uint64 | ||
|
||
参数列表 | ||
- name string flag名称 | ||
- value uint64 变量默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
- *uint64 返回一个uint64类型的flag值的地址 | ||
|
||
功能说明 | ||
- 定义一个带默认值和提示语句的uint64类型flag,返回对应值的地址 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var uint64Flag = flag.Uint64("uint64", 100, "help message for uint") | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Println("uint64Flag: ", *uint64Flag) | ||
} | ||
|
||
代码输出 | ||
|
||
// ./testuint64 | ||
uint64Flag: 100 | ||
|
||
// ./testuint64 -uint64 | ||
flag needs an argument: -uint64 | ||
Usage of ./testuint64: | ||
-uint64=100: help message for uint | ||
|
||
// ./testuint64 -uint64 1231312 | ||
uint64Flag: 1231312 | ||
|
||
// ./testuint64 -uint64=1231312 | ||
uint64Flag: 1231312 |
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,49 @@ | ||
## func Uint64Var(p *uint64, name string, value uint64, usage string) | ||
|
||
参数列表 | ||
- p *uint64 需要与flag参数值绑定的变量地址 | ||
- name string flag名称 | ||
- value uint64 变量默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
|
||
功能说明 | ||
- 将命令行指定flag参数值绑定到int变量 | ||
|
||
示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var uint64varFlag uint64 | ||
|
||
func init() { | ||
flag.Uint64Var(&uint64varFlag, "flag", 123, "help message for uint64var") | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Println("uint64varFlag:", uint64varFlag) | ||
} | ||
|
||
|
||
代码输出 | ||
|
||
//./testuint64var | ||
uint64varFlag: 123 | ||
|
||
// ./testuint64var -flag | ||
flag needs an argument: -flag | ||
Usage of ./testuint64var: | ||
-flag=123: help message for uint64var | ||
|
||
// ./testuint64var -flag 1234 | ||
uint64varFlag: 1234 | ||
|
||
// ./testuint64var -flag=11234 | ||
uint64varFlag: 11234 |
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,48 @@ | ||
## func UintVar(p *uint, name string, value uint, usage string) | ||
|
||
参数列表 | ||
- p *uint 需要与flag参数值绑定的变量地址 | ||
- name string flag名称 | ||
- value uint 变量默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
|
||
功能说明 | ||
- 将命令行指定flag参数值绑定到uint变量 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var uintvarFlag uint | ||
|
||
func init() { | ||
flag.UintVar(&uintvarFlag, "flag", 123, "help message for uintvar") | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Println("uintvarFlag:", uintvarFlag) | ||
} | ||
|
||
代码输出 | ||
|
||
//./testuintvar | ||
uintvarFlag: 123 | ||
|
||
// ./testuintvar -flag | ||
flag needs an argument: -flag | ||
Usage of ./testuintvar: | ||
-flag=123: help message for uintvar | ||
|
||
// ./testuintvar -flag 456 | ||
uintvarFlag: 456 | ||
|
||
// ./testuintvar -flag=456 | ||
uintvarFlag: 456 |
Oops, something went wrong.