Golang expvar.Map type, NewMap(), Add(), AddFloat(), Do(), Get(), Init(), Set() and String() functions examples
package expvar
Map type is a string-to-Var map variable that satisfies the Var interface.
AddFloat function adds delta to the *Float value stored under the given map key.
Do function calls f (1st parameter) for each entry in the map. The map is locked during the iteration, but existing entries may be concurrently updated.
Golang expvar.Map type, NewMap(), Add(), AddFloat(), Do(), Get(), Init(), Set() and String() functions usage examples
package main
import (
"fmt"
"net/http"
"expvar"
)
func kvfunc(kv expvar.KeyValue) {
fmt.Println(kv.Key, kv.Value)
}
func main() {
var inerInt int64 = 10
pubInt := expvar.NewInt("Int")
pubInt.Set(inerInt)
pubInt.Add(2)
var inerFloat float64 = 1.2
pubFloat := expvar.NewFloat("Float")
pubFloat.Set(inerFloat)
pubFloat.Add(0.1)
var inerString string = "hello gophers"
pubString := expvar.NewString("String")
pubString.Set(inerString)
pubMap := expvar.NewMap("Map").Init() // <-- here
pubMap.Set("Int", pubInt)
pubMap.Set("Float", pubFloat)
pubMap.Set("String", pubString)
pubMap.Add("Int", 1)
pubMap.Add("NewInt", 123)
pubMap.AddFloat("Float", 0.5)
pubMap.AddFloat("NewFloat", 0.9)
// getInt := pubMap.Get("Int") // just for demo
pubMap.Do(kvfunc)
expvar.Do(kvfunc) // <---- here
http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "hello gophers")
})
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
References :
http://golang.org/pkg/expvar/#Map
http://golang.org/pkg/expvar/#NewMap
http://golang.org/pkg/expvar/#Map.Add
http://golang.org/pkg/expvar/#Map.AddFloat
Advertisement
Something interesting
Tutorials
+4.4k Golang : Valued expressions and functions example
+19k Golang : Padding data for encryption and un-padding data for decryption
+17.8k Golang : Iterate linked list example
+14.6k Golang : Missing Bazaar command
+7.9k Swift : Convert (cast) String to Float
+7.1k Golang : Array mapping with Interface
+6.2k Golang : Process non-XML/JSON formatted ASCII text file example
+25.3k Golang : Convert uint value to string type
+8k Findstr command the Grep equivalent for Windows
+16.3k Golang : convert string or integer to big.Int type
+33k Golang : How to check if a date is within certain range?
+41k Golang : How to check if a string contains another sub-string?