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
+51.4k Golang : Check if item is in slice/array
+5k Python : Convert(cast) bytes to string example
+9.6k Golang : Validate IPv6 example
+8.8k Golang : Gorilla web tool kit schema example
+4.7k JavaScript: Add marker function on Google Map
+5.4k Python : Delay with time.sleep() function example
+8.7k Golang : Combine slices but preserve order example
+6.5k Golang : Spell checking with ispell example
+15.7k Golang : Get checkbox or extract multipart form data value example
+7.1k Golang : Squaring elements in array
+80.7k Golang : How to return HTTP status code?
+15.6k Golang : Validate hostname