ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# package expvar `import "expvar"` expvar包提供了公共变量的标准接口,如服务的操作计数器。本包通过HTTP在/debug/vars位置以JSON格式导出了这些变量。 对这些公共变量的读写操作都是原子级的。 为了增加HTTP处理器,本包注册了如下变量: ``` cmdline os.Args memstats runtime.Memstats ``` 有时候本包被导入只是为了获得本包注册HTTP处理器和上述变量的副作用。此时可以如下方式导入本包: ``` import _ "expvar" ``` ## Index * [type Var](#Var) * [type Int](#Int) * [func NewInt(name string) \*Int](#NewInt) * [func (v \*Int) Add(delta int64)](#Int.Add) * [func (v \*Int) Set(value int64)](#Int.Set) * [func (v \*Int) String() string](#Int.String) * [type Float](#Float) * [func NewFloat(name string) \*Float](#NewFloat) * [func (v \*Float) Add(delta float64)](#Float.Add) * [func (v \*Float) Set(value float64)](#Float.Set) * [func (v \*Float) String() string](#Float.String) * [type String](#String) * [func NewString(name string) \*String](#NewString) * [func (v \*String) Set(value string)](#String.Set) * [func (v \*String) String() string](#String.String) * [type Func](#Func) * [func (f Func) String() string](#Func.String) * [type KeyValue](#KeyValue) * [type Map](#Map) * [func NewMap(name string) \*Map](#NewMap) * [func (v \*Map) Init() \*Map](#Map.Init) * [func (v \*Map) Get(key string) Var](#Map.Get) * [func (v \*Map) Set(key string, av Var)](#Map.Set) * [func (v \*Map) Add(key string, delta int64)](#Map.Add) * [func (v \*Map) AddFloat(key string, delta float64)](#Map.AddFloat) * [func (v \*Map) Do(f func(KeyValue))](#Map.Do) * [func (v \*Map) String() string](#Map.String) * [func Get(name string) Var](#Get) * [func Publish(name string, v Var)](#Publish) * [func Do(f func(KeyValue))](#Do) ## type [Var](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L38 "View Source") ``` type Var interface { String() string } ``` Var接口是所有导出变量的抽象类型。 ## type [Int](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L43 "View Source") ``` type Int struct { // 内含隐藏或非导出字段 } ``` Int代表一个64位整数变量,满足Var接口。 ### func [NewInt](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L273 "View Source") ``` func NewInt(name string) *Int ``` ### func (\*Int) [Add](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L54 "View Source") ``` func (v *Int) Add(delta int64) ``` ### func (\*Int) [Set](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L60 "View Source") ``` func (v *Int) Set(value int64) ``` ### func (\*Int) [String](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L48 "View Source") ``` func (v *Int) String() string ``` ## type [Float](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L67 "View Source") ``` type Float struct { // 内含隐藏或非导出字段 } ``` Float代表一个64位浮点数变量,满足Var接口。 ### func [NewFloat](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L279 "View Source") ``` func NewFloat(name string) *Float ``` ### func (\*Float) [Add](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L79 "View Source") ``` func (v *Float) Add(delta float64) ``` Add adds delta to v. ### func (\*Float) [Set](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L86 "View Source") ``` func (v *Float) Set(value float64) ``` Set sets v to value. ### func (\*Float) [String](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L72 "View Source") ``` func (v *Float) String() string ``` ## type [String](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L217 "View Source") ``` type String struct { // 内含隐藏或非导出字段 } ``` String代表一个字符串变量,满足Var接口。 ### func [NewString](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L291 "View Source") ``` func NewString(name string) *String ``` ### func (\*String) [Set](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L228 "View Source") ``` func (v *String) Set(value string) ``` ### func (\*String) [String](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L222 "View Source") ``` func (v *String) String() string ``` ## type [Func](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L236 "View Source") ``` type Func func() interface{} ``` Func通过调用函数并将结果编码为json,实现了Var接口。 ### func (Func) [String](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L238 "View Source") ``` func (f Func) String() string ``` ## type [KeyValue](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L100 "View Source") ``` type KeyValue struct { Key string Value Var } ``` KeyValue代表Map中的一条记录。(键值对) ## type [Map](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L93 "View Source") ``` type Map struct { // 内含隐藏或非导出字段 } ``` Map代表一个string到Var的映射变量,满足Var接口。 ### func [NewMap](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L285 "View Source") ``` func NewMap(name string) *Map ``` ### func (\*Map) [Init](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L122 "View Source") ``` func (v *Map) Init() *Map ``` ### func (\*Map) [Get](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L141 "View Source") ``` func (v *Map) Get(key string) Var ``` ### func (\*Map) [Set](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L147 "View Source") ``` func (v *Map) Set(key string, av Var) ``` ### func (\*Map) [Add](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L154 "View Source") ``` func (v *Map) Add(key string, delta int64) ``` ### func (\*Map) [AddFloat](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L177 "View Source") ``` func (v *Map) AddFloat(key string, delta float64) ``` AddFloat向索引key对应的值(底层为\*Float)修改为加上delta后的值。 ### func (\*Map) [Do](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L202 "View Source") ``` func (v *Map) Do(f func(KeyValue)) ``` Do对映射的每一条记录都调用f。迭代执行时会锁定该映射,但已存在的记录可以同时更新。 ### func (\*Map) [String](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L105 "View Source") ``` func (v *Map) String() string ``` ## func [Get](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L265 "View Source") ``` func Get(name string) Var ``` Get获取名为name的导出变量。 ## func [Publish](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L253 "View Source") ``` func Publish(name string, v Var) ``` Publish声明一个导出变量。必须在init函数里调用。如果name已经被注册,会调用log.Panic。 ## func [Do](https://github.com/golang/go/blob/master/src/expvar/expvar.go#L300 "View Source") ``` func Do(f func(KeyValue)) ``` Do对导出变量的每一条记录都调用f。迭代执行时会锁定全局变量映射,但已存在的记录可以同时更新。