Golang expvar.KeyValue type example
package expvar
KeyValue represents a single entry in a Map.
Golang expvar.KeyValue type usage example
package main
import (
"fmt"
"net/http"
"expvar"
)
func kvfunc(kv expvar.KeyValue) { // <-- here
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()
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)
pubMap.Do(kvfunc)
expvar.Do(kvfunc)
http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "hello gophers")
})
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
Reference :
Advertisement
Something interesting
Tutorials
+9.1k Golang : Simple histogram example
+11.6k Golang : Simple file scaning and remove virus example
+10.6k Golang : Flip coin example
+6.1k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+16.3k Golang : How to extract links from web page ?
+6.7k Golang : Experimental emojis or emoticons icons programming language
+15.2k Golang : Accurate and reliable decimal calculations
+19.6k Golang : Set or Add HTTP Request Headers
+6.1k Golang : Grab news article text and use NLP to get each paragraph's sentences
+23k Golang : Calculate time different
+15.6k Golang : Validate hostname
+6.9k Mac OSX : Find large files by size