# package flag
`import "flag"`
flag包實現了命令行參數的解析。
要求:
使用flag.String(), Bool(), Int()等函數注冊flag,下例聲明了一個整數flag,解析結果保存在\*int指針ip里:
```
import "flag"
var ip = flag.Int("flagname", 1234, "help message for flagname")
```
如果你喜歡,也可以將flag綁定到一個變量,使用Var系列函數:
```
var flagvar int
func init() {
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
}
```
或者你可以自定義一個用于flag的類型(滿足Value接口)并將該類型用于flag解析,如下:
```
flag.Var(&flagVal, "name", "help message for flagname")
```
對這種flag,默認值就是該變量的初始值。
在所有flag都注冊之后,調用:
```
flag.Parse()
```
來解析命令行參數寫入注冊的flag里。
解析之后,flag的值可以直接使用。如果你使用的是flag自身,它們是指針;如果你綁定到了某個變量,它們是值。
```
fmt.Println("ip has value ", *ip)
fmt.Println("flagvar has value ", flagvar)
```
解析后,flag后面的參數可以從flag.Args()里獲取或用flag.Arg(i)單獨獲取。這些參數的索引為從0到flag.NArg()-1。
命令行flag語法:
```
-flag
-flag=x
-flag x // 只有非bool類型的flag可以
```
可以使用1個或2個'-'號,效果是一樣的。最后一種格式不能用于bool類型的flag,因為如果有文件名為0、false等時,如下命令:
```
cmd -x *
```
其含義會改變。你必須使用-flag=false格式來關閉一個bool類型flag。
Flag解析在第一個非flag參數(單個"-"不是flag參數)之前停止,或者在終止符"--"之后停止。
整數flag接受1234、0664、0x1234等類型,也可以是負數。bool類型flag可以是:
```
1, 0, t, f, T, F, true, false, TRUE, FALSE, True, False
```
時間段flag接受任何合法的可提供給time.ParseDuration的輸入。
默認的命令行flag集被包水平的函數控制。FlagSet類型允許程序員定義獨立的flag集,例如實現命令行界面下的子命令。FlagSet的方法和包水平的函數是非常類似的。
Example
```
// These examples demonstrate more intricate uses of the flag package.
package flag_test
import (
"errors"
"flag"
"fmt"
"strings"
"time"
)
// Example 1: A single string flag called "species" with default value "gopher".
var species = flag.String("species", "gopher", "the species we are studying")
// Example 2: Two flags sharing a variable, so we can have a shorthand.
// The order of initialization is undefined, so make sure both use the
// same default value. They must be set up with an init function.
var gopherType string
func init() {
const (
defaultGopher = "pocket"
usage = "the variety of gopher"
)
flag.StringVar(&gopherType, "gopher_type", defaultGopher, usage)
flag.StringVar(&gopherType, "g", defaultGopher, usage+" (shorthand)")
}
// Example 3: A user-defined flag type, a slice of durations.
type interval []time.Duration
// String is the method to format the flag's value, part of the flag.Value interface.
// The String method's output will be used in diagnostics.
func (i *interval) String() string {
return fmt.Sprint(*i)
}
// Set is the method to set the flag value, part of the flag.Value interface.
// Set's argument is a string to be parsed to set the flag.
// It's a comma-separated list, so we split it.
func (i *interval) Set(value string) error {
// If we wanted to allow the flag to be set multiple times,
// accumulating values, we would delete this if statement.
// That would permit usages such as
// -deltaT 10s -deltaT 15s
// and other combinations.
if len(*i) > 0 {
return errors.New("interval flag already set")
}
for _, dt := range strings.Split(value, ",") {
duration, err := time.ParseDuration(dt)
if err != nil {
return err
}
*i = append(*i, duration)
}
return nil
}
// Define a flag to accumulate durations. Because it has a special type,
// we need to use the Var function and therefore create the flag during
// init.
var intervalFlag interval
func init() {
// Tie the command-line flag to the intervalFlag variable and
// set a usage message.
flag.Var(&intervalFlag, "deltaT", "comma-separated list of intervals to use between events")
}
func Example() {
// All the interesting pieces are with the variables declared above, but
// to enable the flag package to see the flags defined there, one must
// execute, typically at the start of main (not init!):
// flag.Parse()
// We don't run it here because this is not a main function and
// the testing suite has already parsed the flags.
}
```
## Index
* [Variables](#pkg-variables)
* [type Value](#Value)
* [type Getter](#Getter)
* [type ErrorHandling](#ErrorHandling)
* [type Flag](#Flag)
* [type FlagSet](#FlagSet)
* [func NewFlagSet(name string, errorHandling ErrorHandling) \*FlagSet](#NewFlagSet)
* [func (f \*FlagSet) Init(name string, errorHandling ErrorHandling)](#FlagSet.Init)
* [func (f \*FlagSet) NFlag() int](#FlagSet.NFlag)
* [func (f \*FlagSet) Lookup(name string) \*Flag](#FlagSet.Lookup)
* [func (f \*FlagSet) NArg() int](#FlagSet.NArg)
* [func (f \*FlagSet) Args() []string](#FlagSet.Args)
* [func (f \*FlagSet) Arg(i int) string](#FlagSet.Arg)
* [func (f \*FlagSet) PrintDefaults()](#FlagSet.PrintDefaults)
* [func (f \*FlagSet) SetOutput(output io.Writer)](#FlagSet.SetOutput)
* [func (f \*FlagSet) Bool(name string, value bool, usage string) \*bool](#FlagSet.Bool)
* [func (f \*FlagSet) BoolVar(p \*bool, name string, value bool, usage string)](#FlagSet.BoolVar)
* [func (f \*FlagSet) Int(name string, value int, usage string) \*int](#FlagSet.Int)
* [func (f \*FlagSet) IntVar(p \*int, name string, value int, usage string)](#FlagSet.IntVar)
* [func (f \*FlagSet) Int64(name string, value int64, usage string) \*int64](#FlagSet.Int64)
* [func (f \*FlagSet) Int64Var(p \*int64, name string, value int64, usage string)](#FlagSet.Int64Var)
* [func (f \*FlagSet) Uint(name string, value uint, usage string) \*uint](#FlagSet.Uint)
* [func (f \*FlagSet) UintVar(p \*uint, name string, value uint, usage string)](#FlagSet.UintVar)
* [func (f \*FlagSet) Uint64(name string, value uint64, usage string) \*uint64](#FlagSet.Uint64)
* [func (f \*FlagSet) Uint64Var(p \*uint64, name string, value uint64, usage string)](#FlagSet.Uint64Var)
* [func (f \*FlagSet) Float64(name string, value float64, usage string) \*float64](#FlagSet.Float64)
* [func (f \*FlagSet) Float64Var(p \*float64, name string, value float64, usage string)](#FlagSet.Float64Var)
* [func (f \*FlagSet) String(name string, value string, usage string) \*string](#FlagSet.String)
* [func (f \*FlagSet) StringVar(p \*string, name string, value string, usage string)](#FlagSet.StringVar)
* [func (f \*FlagSet) Duration(name string, value time.Duration, usage string) \*time.Duration](#FlagSet.Duration)
* [func (f \*FlagSet) DurationVar(p \*time.Duration, name string, value time.Duration, usage string)](#FlagSet.DurationVar)
* [func (f \*FlagSet) Var(value Value, name string, usage string)](#FlagSet.Var)
* [func (f \*FlagSet) Set(name, value string) error](#FlagSet.Set)
* [func (f \*FlagSet) Parse(arguments []string) error](#FlagSet.Parse)
* [func (f \*FlagSet) Parsed() bool](#FlagSet.Parsed)
* [func (f \*FlagSet) Visit(fn func(\*Flag))](#FlagSet.Visit)
* [func (f \*FlagSet) VisitAll(fn func(\*Flag))](#FlagSet.VisitAll)
* [func NFlag() int](#NFlag)
* [func Lookup(name string) \*Flag](#Lookup)
* [func NArg() int](#NArg)
* [func Args() []string](#Args)
* [func Arg(i int) string](#Arg)
* [func PrintDefaults()](#PrintDefaults)
* [func Bool(name string, value bool, usage string) \*bool](#Bool)
* [func BoolVar(p \*bool, name string, value bool, usage string)](#BoolVar)
* [func Int(name string, value int, usage string) \*int](#Int)
* [func IntVar(p \*int, name string, value int, usage string)](#IntVar)
* [func Int64(name string, value int64, usage string) \*int64](#Int64)
* [func Int64Var(p \*int64, name string, value int64, usage string)](#Int64Var)
* [func Uint(name string, value uint, usage string) \*uint](#Uint)
* [func UintVar(p \*uint, name string, value uint, usage string)](#UintVar)
* [func Uint64(name string, value uint64, usage string) \*uint64](#Uint64)
* [func Uint64Var(p \*uint64, name string, value uint64, usage string)](#Uint64Var)
* [func Float64(name string, value float64, usage string) \*float64](#Float64)
* [func Float64Var(p \*float64, name string, value float64, usage string)](#Float64Var)
* [func String(name string, value string, usage string) \*string](#String)
* [func StringVar(p \*string, name string, value string, usage string)](#StringVar)
* [func Duration(name string, value time.Duration, usage string) \*time.Duration](#Duration)
* [func DurationVar(p \*time.Duration, name string, value time.Duration, usage string)](#DurationVar)
* [func Var(value Value, name string, usage string)](#Var)
* [func Set(name, value string) error](#Set)
* [func Parse()](#Parse)
* [func Parsed() bool](#Parsed)
* [func Visit(fn func(\*Flag))](#Visit)
* [func VisitAll(fn func(\*Flag))](#VisitAll)
### Examples
* [package](#example-package)
## Variables
```
var CommandLine = NewFlagSet(os.Args[0], ExitOnError)
```
CommandLine是默認的命令行flag集,用于解析os.Args。包水平的函數如BoolVar、Arg等都是對其方法的封裝。
```
var ErrHelp = errors.New("flag: help requested")
```
當flag -help被調用但沒有注冊這個flag時,就會返回ErrHelp。
```
var Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
PrintDefaults()
}
```
Usage打印到標準錯誤輸出一個使用信息,記錄了所有注冊的flag。本函數是一個包變量,可以將其修改為指向自定義的函數。
## type [Value](https://github.com/golang/go/blob/master/src/flag/flag.go#L237 "View Source")
```
type Value interface {
String() string
Set(string) error
}
```
Value接口是用于將動態的值保存在一個flag里。(默認值被表示為一個字符串)
如果Value接口具有IsBoolFlag() bool方法,且返回真。命令行解析其會將-name等價為-name=true而不是使用下一個命令行參數。
## type [Getter](https://github.com/golang/go/blob/master/src/flag/flag.go#L246 "View Source")
```
type Getter interface {
Value
Get() interface{}
}
```
Gette接口使可以取回Value接口的內容。本接口包裝了Value接口而不是作為Value接口的一部分,因為本接口是在Go 1之后出現,出于兼容的考慮才如此。本包所有的滿足Value接口的類型都同時滿足Getter接口。
## type [ErrorHandling](https://github.com/golang/go/blob/master/src/flag/flag.go#L252 "View Source")
```
type ErrorHandling int
```
ErrorHandling定義如何處理flag解析錯誤。
```
const (
ContinueOnError ErrorHandling = iota
ExitOnError
PanicOnError
)
```
## type [Flag](https://github.com/golang/go/blob/master/src/flag/flag.go#L278 "View Source")
```
type Flag struct {
Name string // flag在命令行中的名字
Usage string // 幫助信息
Value Value // 要設置的值
DefValue string // 默認值(文本格式),用于使用信息
}
```
Flag類型代表一條flag的狀態。
## type [FlagSet](https://github.com/golang/go/blob/master/src/flag/flag.go#L262 "View Source")
```
type FlagSet struct {
// Usage函數在解析flag出現錯誤時會被調用
// 該字段為一個函數(而非采用方法),以便修改為自定義的錯誤處理函數
Usage func()
// 內含隱藏或非導出字段
}
```
FlagSet代表一個已注冊的flag的集合。FlagSet零值沒有名字,采用ContinueOnError錯誤處理策略。
### func [NewFlagSet](https://github.com/golang/go/blob/master/src/flag/flag.go#L835 "View Source")
```
func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet
```
NewFlagSet創建一個新的、名為name,采用errorHandling為錯誤處理策略的FlagSet。
### func (\*FlagSet) [Init](https://github.com/golang/go/blob/master/src/flag/flag.go#L846 "View Source")
```
func (f *FlagSet) Init(name string, errorHandling ErrorHandling)
```
Init設置flag集合f的名字和錯誤處理屬性。FlagSet零值沒有名字,默認采用ContinueOnError錯誤處理策略。
### func (\*FlagSet) [NFlag](https://github.com/golang/go/blob/master/src/flag/flag.go#L415 "View Source")
```
func (f *FlagSet) NFlag() int
```
NFlag返回解析時進行了設置的flag的數量。
### func (\*FlagSet) [Lookup](https://github.com/golang/go/blob/master/src/flag/flag.go#L343 "View Source")
```
func (f *FlagSet) Lookup(name string) *Flag
```
返回已經f中已注冊flag的Flag結構體指針;如果flag不存在的話,返回nil。
### func (\*FlagSet) [NArg](https://github.com/golang/go/blob/master/src/flag/flag.go#L436 "View Source")
```
func (f *FlagSet) NArg() int
```
NArg返回解析flag之后剩余參數的個數。
### func (\*FlagSet) [Args](https://github.com/golang/go/blob/master/src/flag/flag.go#L442 "View Source")
```
func (f *FlagSet) Args() []string
```
返回解析之后剩下的非flag參數。(不包括命令名)
### func (\*FlagSet) [Arg](https://github.com/golang/go/blob/master/src/flag/flag.go#L422 "View Source")
```
func (f *FlagSet) Arg(i int) string
```
返回解析之后剩下的第i個參數,從0開始索引。
### func (\*FlagSet) [PrintDefaults](https://github.com/golang/go/blob/master/src/flag/flag.go#L377 "View Source")
```
func (f *FlagSet) PrintDefaults()
```
PrintDefault打印集合中所有注冊好的flag的默認值。除非另外配置,默認輸出到標準錯誤輸出中。
### func (\*FlagSet) [SetOutput](https://github.com/golang/go/blob/master/src/flag/flag.go#L310 "View Source")
```
func (f *FlagSet) SetOutput(output io.Writer)
```
設置使用信息和錯誤信息的輸出流,如果output為nil,將使用os.Stderr。
### func (\*FlagSet) [Bool](https://github.com/golang/go/blob/master/src/flag/flag.go#L461 "View Source")
```
func (f *FlagSet) Bool(name string, value bool, usage string) *bool
```
Bool用指定的名稱、默認值、使用信息注冊一個bool類型flag。返回一個保存了該flag的值的指針。
### func (\*FlagSet) [BoolVar](https://github.com/golang/go/blob/master/src/flag/flag.go#L449 "View Source")
```
func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string)
```
BoolVar用指定的名稱、默認值、使用信息注冊一個bool類型flag,并將flag的值保存到p指向的變量。
### func (\*FlagSet) [Int](https://github.com/golang/go/blob/master/src/flag/flag.go#L487 "View Source")
```
func (f *FlagSet) Int(name string, value int, usage string) *int
```
Int用指定的名稱、默認值、使用信息注冊一個int類型flag。返回一個保存了該flag的值的指針。
### func (\*FlagSet) [IntVar](https://github.com/golang/go/blob/master/src/flag/flag.go#L475 "View Source")
```
func (f *FlagSet) IntVar(p *int, name string, value int, usage string)
```
IntVar用指定的名稱、默認值、使用信息注冊一個int類型flag,并將flag的值保存到p指向的變量。
### func (\*FlagSet) [Int64](https://github.com/golang/go/blob/master/src/flag/flag.go#L513 "View Source")
```
func (f *FlagSet) Int64(name string, value int64, usage string) *int64
```
Int64用指定的名稱、默認值、使用信息注冊一個int64類型flag。返回一個保存了該flag的值的指針。
### func (\*FlagSet) [Int64Var](https://github.com/golang/go/blob/master/src/flag/flag.go#L501 "View Source")
```
func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string)
```
Int64Var用指定的名稱、默認值、使用信息注冊一個int64類型flag,并將flag的值保存到p指向的變量。
### func (\*FlagSet) [Uint](https://github.com/golang/go/blob/master/src/flag/flag.go#L539 "View Source")
```
func (f *FlagSet) Uint(name string, value uint, usage string) *uint
```
Uint用指定的名稱、默認值、使用信息注冊一個uint類型flag。返回一個保存了該flag的值的指針。
### func (\*FlagSet) [UintVar](https://github.com/golang/go/blob/master/src/flag/flag.go#L527 "View Source")
```
func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string)
```
UintVar用指定的名稱、默認值、使用信息注冊一個uint類型flag,并將flag的值保存到p指向的變量。
### func (\*FlagSet) [Uint64](https://github.com/golang/go/blob/master/src/flag/flag.go#L565 "View Source")
```
func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64
```
Uint64用指定的名稱、默認值、使用信息注冊一個uint64類型flag。返回一個保存了該flag的值的指針。
### func (\*FlagSet) [Uint64Var](https://github.com/golang/go/blob/master/src/flag/flag.go#L553 "View Source")
```
func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string)
```
Uint64Var用指定的名稱、默認值、使用信息注冊一個uint64類型flag,并將flag的值保存到p指向的變量。
### func (\*FlagSet) [Float64](https://github.com/golang/go/blob/master/src/flag/flag.go#L617 "View Source")
```
func (f *FlagSet) Float64(name string, value float64, usage string) *float64
```
Float64用指定的名稱、默認值、使用信息注冊一個float64類型flag。返回一個保存了該flag的值的指針。
### func (\*FlagSet) [Float64Var](https://github.com/golang/go/blob/master/src/flag/flag.go#L605 "View Source")
```
func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string)
```
Float64Var用指定的名稱、默認值、使用信息注冊一個float64類型flag,并將flag的值保存到p指向的變量。
### func (\*FlagSet) [String](https://github.com/golang/go/blob/master/src/flag/flag.go#L591 "View Source")
```
func (f *FlagSet) String(name string, value string, usage string) *string
```
String用指定的名稱、默認值、使用信息注冊一個string類型flag。返回一個保存了該flag的值的指針。
### func (\*FlagSet) [StringVar](https://github.com/golang/go/blob/master/src/flag/flag.go#L579 "View Source")
```
func (f *FlagSet) StringVar(p *string, name string, value string, usage string)
```
StringVar用指定的名稱、默認值、使用信息注冊一個string類型flag,并將flag的值保存到p指向的變量。
### func (\*FlagSet) [Duration](https://github.com/golang/go/blob/master/src/flag/flag.go#L643 "View Source")
```
func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration
```
Duration用指定的名稱、默認值、使用信息注冊一個time.Duration類型flag。返回一個保存了該flag的值的指針。
### func (\*FlagSet) [DurationVar](https://github.com/golang/go/blob/master/src/flag/flag.go#L631 "View Source")
```
func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string)
```
DurationVar用指定的名稱、默認值、使用信息注冊一個time.Duration類型flag,并將flag的值保存到p指向的變量。
### func (\*FlagSet) [Var](https://github.com/golang/go/blob/master/src/flag/flag.go#L661 "View Source")
```
func (f *FlagSet) Var(value Value, name string, usage string)
```
Var方法使用指定的名字、使用信息注冊一個flag。該flag的類型和值由第一個參數表示,該參數應實現了Value接口。例如,用戶可以創建一個flag,可以用Value接口的Set方法將逗號分隔的字符串轉化為字符串切片。
### func (\*FlagSet) [Set](https://github.com/golang/go/blob/master/src/flag/flag.go#L354 "View Source")
```
func (f *FlagSet) Set(name, value string) error
```
設置已注冊的flag的值。
### func (\*FlagSet) [Parse](https://github.com/golang/go/blob/master/src/flag/flag.go#L788 "View Source")
```
func (f *FlagSet) Parse(arguments []string) error
```
從arguments中解析注冊的flag。必須在所有flag都注冊好而未訪問其值時執行。未注冊卻使用flag -help時,會返回ErrHelp。
### func (\*FlagSet) [Parsed](https://github.com/golang/go/blob/master/src/flag/flag.go#L812 "View Source")
```
func (f *FlagSet) Parsed() bool
```
返回是否f.Parse已經被調用過。
### func (\*FlagSet) [Visit](https://github.com/golang/go/blob/master/src/flag/flag.go#L330 "View Source")
```
func (f *FlagSet) Visit(fn func(*Flag))
```
按照字典順序遍歷標簽,并且對每個標簽調用fn。?這個函數只遍歷解析時進行了設置的標簽。
### func (\*FlagSet) [VisitAll](https://github.com/golang/go/blob/master/src/flag/flag.go#L316 "View Source")
```
func (f *FlagSet) VisitAll(fn func(*Flag))
```
按照字典順序遍歷標簽,并且對每個標簽調用fn。?這個函數會遍歷所有標簽,不管解析時有無進行設置。
## func [NFlag](https://github.com/golang/go/blob/master/src/flag/flag.go#L418 "View Source")
```
func NFlag() int
```
NFlag返回已被設置的flag的數量。
## func [Lookup](https://github.com/golang/go/blob/master/src/flag/flag.go#L349 "View Source")
```
func Lookup(name string) *Flag
```
返回已經已注冊flag的Flag結構體指針;如果flag不存在的話,返回nil。。
## func [NArg](https://github.com/golang/go/blob/master/src/flag/flag.go#L439 "View Source")
```
func NArg() int
```
NArg返回解析flag之后剩余參數的個數。
## func [Args](https://github.com/golang/go/blob/master/src/flag/flag.go#L445 "View Source")
```
func Args() []string
```
返回解析之后剩下的非flag參數。(不包括命令名)
## func [Arg](https://github.com/golang/go/blob/master/src/flag/flag.go#L431 "View Source")
```
func Arg(i int) string
```
返回解析之后剩下的第i個參數,從0開始索引。
## func [PrintDefaults](https://github.com/golang/go/blob/master/src/flag/flag.go#L389 "View Source")
```
func PrintDefaults()
```
PrintDefault會向標準錯誤輸出寫入所有注冊好的flag的默認值。
## func [Bool](https://github.com/golang/go/blob/master/src/flag/flag.go#L469 "View Source")
```
func Bool(name string, value bool, usage string) *bool
```
Bool用指定的名稱、默認值、使用信息注冊一個bool類型flag。返回一個保存了該flag的值的指針。
## func [BoolVar](https://github.com/golang/go/blob/master/src/flag/flag.go#L455 "View Source")
```
func BoolVar(p *bool, name string, value bool, usage string)
```
BoolVar用指定的名稱、默認值、使用信息注冊一個bool類型flag,并將flag的值保存到p指向的變量。
## func [Int](https://github.com/golang/go/blob/master/src/flag/flag.go#L495 "View Source")
```
func Int(name string, value int, usage string) *int
```
Int用指定的名稱、默認值、使用信息注冊一個int類型flag。返回一個保存了該flag的值的指針。
## func [IntVar](https://github.com/golang/go/blob/master/src/flag/flag.go#L481 "View Source")
```
func IntVar(p *int, name string, value int, usage string)
```
IntVar用指定的名稱、默認值、使用信息注冊一個int類型flag,并將flag的值保存到p指向的變量。
## func [Int64](https://github.com/golang/go/blob/master/src/flag/flag.go#L521 "View Source")
```
func Int64(name string, value int64, usage string) *int64
```
Int64用指定的名稱、默認值、使用信息注冊一個int64類型flag。返回一個保存了該flag的值的指針。
## func [Int64Var](https://github.com/golang/go/blob/master/src/flag/flag.go#L507 "View Source")
```
func Int64Var(p *int64, name string, value int64, usage string)
```
Int64Var用指定的名稱、默認值、使用信息注冊一個int64類型flag,并將flag的值保存到p指向的變量。
## func [Uint](https://github.com/golang/go/blob/master/src/flag/flag.go#L547 "View Source")
```
func Uint(name string, value uint, usage string) *uint
```
Uint用指定的名稱、默認值、使用信息注冊一個uint類型flag。返回一個保存了該flag的值的指針。
## func [UintVar](https://github.com/golang/go/blob/master/src/flag/flag.go#L533 "View Source")
```
func UintVar(p *uint, name string, value uint, usage string)
```
UintVar用指定的名稱、默認值、使用信息注冊一個uint類型flag,并將flag的值保存到p指向的變量。
## func [Uint64](https://github.com/golang/go/blob/master/src/flag/flag.go#L573 "View Source")
```
func Uint64(name string, value uint64, usage string) *uint64
```
Uint64用指定的名稱、默認值、使用信息注冊一個uint64類型flag。返回一個保存了該flag的值的指針。
## func [Uint64Var](https://github.com/golang/go/blob/master/src/flag/flag.go#L559 "View Source")
```
func Uint64Var(p *uint64, name string, value uint64, usage string)
```
Uint64Var用指定的名稱、默認值、使用信息注冊一個uint64類型flag,并將flag的值保存到p指向的變量。
## func [Float64](https://github.com/golang/go/blob/master/src/flag/flag.go#L625 "View Source")
```
func Float64(name string, value float64, usage string) *float64
```
Float64用指定的名稱、默認值、使用信息注冊一個float64類型flag。返回一個保存了該flag的值的指針。
## func [Float64Var](https://github.com/golang/go/blob/master/src/flag/flag.go#L611 "View Source")
```
func Float64Var(p *float64, name string, value float64, usage string)
```
Float64Var用指定的名稱、默認值、使用信息注冊一個float64類型flag,并將flag的值保存到p指向的變量。
## func [String](https://github.com/golang/go/blob/master/src/flag/flag.go#L599 "View Source")
```
func String(name string, value string, usage string) *string
```
String用指定的名稱、默認值、使用信息注冊一個string類型flag。返回一個保存了該flag的值的指針。
## func [StringVar](https://github.com/golang/go/blob/master/src/flag/flag.go#L585 "View Source")
```
func StringVar(p *string, name string, value string, usage string)
```
StringVar用指定的名稱、默認值、使用信息注冊一個string類型flag,并將flag的值保存到p指向的變量。
## func [Duration](https://github.com/golang/go/blob/master/src/flag/flag.go#L651 "View Source")
```
func Duration(name string, value time.Duration, usage string) *time.Duration
```
Duration用指定的名稱、默認值、使用信息注冊一個time.Duration類型flag。返回一個保存了該flag的值的指針。
## func [DurationVar](https://github.com/golang/go/blob/master/src/flag/flag.go#L637 "View Source")
```
func DurationVar(p *time.Duration, name string, value time.Duration, usage string)
```
DurationVar用指定的名稱、默認值、使用信息注冊一個time.Duration類型flag,并將flag的值保存到p指向的變量。
## func [Var](https://github.com/golang/go/blob/master/src/flag/flag.go#L687 "View Source")
```
func Var(value Value, name string, usage string)
```
Var方法使用指定的名字、使用信息注冊一個flag。該flag的類型和值由第一個參數表示,該參數應實現了Value接口。例如,用戶可以創建一個flag,可以用Value接口的Set方法將逗號分隔的字符串轉化為字符串切片。
## func [Set](https://github.com/golang/go/blob/master/src/flag/flag.go#L371 "View Source")
```
func Set(name, value string) error
```
設置已注冊的flag的值。
## func [Parse](https://github.com/golang/go/blob/master/src/flag/flag.go#L818 "View Source")
```
func Parse()
```
從os.Args[1:]中解析注冊的flag。必須在所有flag都注冊好而未訪問其值時執行。未注冊卻使用flag -help時,會返回ErrHelp。
## func [Parsed](https://github.com/golang/go/blob/master/src/flag/flag.go#L824 "View Source")
```
func Parsed() bool
```
返回是否Parse已經被調用過。
## func [Visit](https://github.com/golang/go/blob/master/src/flag/flag.go#L338 "View Source")
```
func Visit(fn func(*Flag))
```
按照字典順序遍歷標簽,并且對每個標簽調用fn。?這個函數只遍歷解析時進行了設置的標簽。
## func [VisitAll](https://github.com/golang/go/blob/master/src/flag/flag.go#L324 "View Source")
```
func VisitAll(fn func(*Flag))
```
按照字典順序遍歷標簽,并且對每個標簽調用fn。?這個函數會遍歷所有標簽,不管解析時有無進行設置。
- 庫
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe