Golang expvar.String type, NewString(), Set() and String() functions example
package expvar
String type is a string variable, and satisfies the Var interface.
Golang expvar.String type, NewString(), Set() and String() functions usage example
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" // <-- here
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)
}
}
References :
Advertisement
Something interesting
Tutorials
+5.5k Golang : Stop goroutine without channel
+8.4k Your page has meta tags in the body instead of the head
+16.4k Golang : Send email and SMTP configuration example
+7.3k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+30.4k Golang : How to verify uploaded file is image or allowed file types
+13.5k Facebook PHP getUser() returns 0
+19.9k Golang : Measure http.Get() execution time
+19.3k Golang : Get host name or domain name from IP address
+21.3k Golang : Create and resolve(read) symbolic links
+12.5k Golang : Arithmetic operation with numerical slices or arrays example
+15.3k Golang : Get query string value on a POST request