Skip to content

Commit

Permalink
增加部分接口demo
Browse files Browse the repository at this point in the history
  • Loading branch information
faberliu committed Nov 9, 2014
1 parent b24024f commit 2bfc6ef
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 2 deletions.
50 changes: 50 additions & 0 deletions flag/FlagSetLookup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## func (f *FlagSet) Lookup(name string) *Flag

参数列表
- name string flag名称

返回值
- *Flag Flag指针

功能说明
- 获取flag集合f中名称为name值的flag指针,如果对应的flag不存在,返回nil

代码示例

package main

import (
"flag"
"fmt"
)

var myFlagSet = flag.NewFlagSet("testFlagSet", flag.ExitOnError)
var testFlag = myFlagSet.String("test", "default value", "help message.")

func print(f *flag.Flag) {
if f != nil {
fmt.Println(f.Value)
} else {
fmt.Println(nil)
}
}

func main() {
fmt.Print("test:")
print(myFlagSet.Lookup("test"))
fmt.Print("test1:")
print(myFlagSet.Lookup("test1"))
myFlagSet.Parse([]string{"-test=12345"})
fmt.Print("test:")
print(myFlagSet.Lookup("test"))
fmt.Print("test1:")
print(myFlagSet.Lookup("test1"))
}

运行结果

// ./testflagsetlookup -test "12345"
test:default value
test1:<nil>
test:12345
test1:<nil>
54 changes: 54 additions & 0 deletions flag/FlagSetNArg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## func (f *FlagSet) NArg() int

参数列表

返回值
- int 返回解析后剩余的参数的数量

功能说明
- 获取输入参数解析后剩余的参数的数量,即fmt.Args()返回的值的元素个数

示例

package main

import (
"flag"
"fmt"
)

const (
defaultUsage = "help message"
)

var (
myFlagSet = flag.NewFlagSet("newflagset", flag.ExitOnError)
uintFlag = myFlagSet.Uint("uintFlag", 1000, defaultUsage)
uintVar uint
)

func init() {
myFlagSet.UintVar(&uintVar, "uintVar", 30, defaultUsage)
}

func main() {
args := []string{
"-uintFlag", "24",
"--uintVar", "25",
"arg1",
"arg2",
"arg3",
}
myFlagSet.Parse(args)
length := myFlagSet.NArg()
for i := 0; i < length; i++ {
fmt.Println(i, myFlagSet.Arg(i))
}
}

代码输出

// ./testflagsetnarg
0 arg1
1 arg2
2 arg3
49 changes: 49 additions & 0 deletions flag/FlagSetNFlag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## func (f *FlagSet) NFlag() int

参数列表

返回值
- int 返回解析成功的flag个数

功能说明
- 获取解析成功的flag个数

示例

package main

import (
"flag"
"fmt"
)

const (
defaultUsage = "help message"
)

var (
myFlagSet = flag.NewFlagSet("newflagset", flag.ExitOnError)
uintFlag = myFlagSet.Uint("uintFlag", 1000, defaultUsage)
testFlag = myFlagSet.String("testFlag", "default value", defaultUsage)
uintVar uint
)

func init() {
myFlagSet.UintVar(&uintVar, "uintVar", 30, defaultUsage)
}

func main() {
args := []string{
"-uintFlag", "24",
"arg1",
}
fmt.Println("before:", myFlagSet.NFlag())
myFlagSet.Parse(args)
fmt.Println("after:", myFlagSet.NFlag())
}

代码输出

// ./testflagsetnflag
before: 0
after: 1
53 changes: 53 additions & 0 deletions flag/FlagSetUint64.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64

参数列表
- name string flag名称
- value uint64 变量默认值
- usage string 提示信息

返回值
- *uint64 返回一个uint64类型的变量的地址

功能说明
- 为flag集合f增加爱一个带默认值和提示语句的uint64类型flag,返回flag对应变量值的地址

代码示例

package main

import (
"flag"
"fmt"
)

const (
defaultUsage = "help message"
)

var (
myFlagSet = flag.NewFlagSet("newflagset", flag.ExitOnError)
uint64Flag = myFlagSet.Uint64("uint64Flag", 3310000, defaultUsage)
uint64Var uint64
)

func init() {
myFlagSet.Uint64Var(&uint64Var, "uint64Var", 719200, defaultUsage)
}

func main() {
args := []string{
"--uint64Var", "25",
"-uint64Flag", "1231",
"arg2",
}
myFlagSet.Parse(args)
fmt.Println("uint64Flag", *uint64Flag)
fmt.Println("uint64Var", uint64Var)
}


代码输出

// ./testflagsetuint64
uint64Flag 1231
uint64Var 25
53 changes: 53 additions & 0 deletions flag/FlagSetUint64Var.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string)

参数列表
- p *uint64 需要与flag参数值绑定的变量地址
- name string flag名称
- value uint64 变量默认值
- usage string 提示信息

返回值

功能说明
- 将f中指定flag参数值绑定到uint64变量

代码示例

package main

import (
"flag"
"fmt"
)

const (
defaultUsage = "help message"
)

var (
myFlagSet = flag.NewFlagSet("newflagset", flag.ExitOnError)
uint64Flag = myFlagSet.Uint64("uint64Flag", 3310000, defaultUsage)
uint64Var uint64
)

func init() {
myFlagSet.Uint64Var(&uint64Var, "uint64Var", 719200, defaultUsage)
}

func main() {
args := []string{
"--uint64Var", "25",
"-uint64Flag", "1231",
"arg2",
}
myFlagSet.Parse(args)
fmt.Println("uint64Flag", *uint64Flag)
fmt.Println("uint64Var", uint64Var)
}


代码输出

// ./testflagsetuint64
uint64Flag 1231
uint64Var 25
49 changes: 49 additions & 0 deletions flag/Lookup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## func Lookup(name string) *Flag

参数列表
- name string flag名称

返回值
- *Flag Flag指针

功能说明
- 获取flag集合中名称为name值的flag指针,如果对应的flag不存在,返回nil

代码示例

package main

import (
"flag"
"fmt"
)

var testFlag = flag.String("test", "default value", "help message.")

func print(f *flag.Flag) {
if f != nil {
fmt.Println(f.Value)
} else {
fmt.Println(nil)
}
}

func main() {
fmt.Print("test:")
print(flag.Lookup("test"))
fmt.Print("test1:")
print(flag.Lookup("test1"))
flag.Parse()
fmt.Print("test:")
print(flag.Lookup("test"))
fmt.Print("test1:")
print(flag.Lookup("test1"))
}

运行结果

// ./testlookup -test "12345"
test:default value
test1:<nil>
test:12345
test1:<nil>
4 changes: 2 additions & 2 deletions flag/NFlag.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
参数列表

返回值
- int 返回命令行已设置的flag个数
- int 返回命令行参数里已解析到的flag个数

功能说明
- 获取命令行已设置的flag个数
- 获取命令行参数里解析成功的flag个数

示例

Expand Down

0 comments on commit 2bfc6ef

Please sign in to comment.