Golang expvar.Var type and Get() function example

package expvar

Var type is an abstract type for all exported variables.

Get function retrieves a named exported variable.

Golang expvar.Var type usage example

 type Graphite struct {
 endpoint string
 interval time.Duration
 timeout time.Duration
 connection net.Conn
 vars map[string]expvar.Var
 registrations chan namedVar
 shutdown chan chan bool
 }

 g := &Graphite{
 endpoint: endpoint,
 interval: interval,
 timeout: timeout,
 connection: nil,
 vars: map[string]expvar.Var{},
 registrations: make(chan namedVar),
 shutdown: make(chan chan bool),
 }

Golang expvar.Get() function usage example

 ...
 reqs := expvar.NewFloat("requests-float")
 if reqs.f != 0.0 {
 fmt.Printf("reqs.f = %v, want 0", reqs.f)
 }

 if reqs != expvar.Get("requests-float").(*Float) {
 fmt.Printf("Get() failed.")
 }
 ...

Advertisement