Golang expvar.Float type, NewFloat(), Add() and Set() functions example
package expvar
Float is a 64-bit float variable that satisfies the Var interface.
Add() function adds delta to v.
Set() function sets v to value.
Golang expvar.Float type, NewFloat(), Add() and Set() 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") // <-- here
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) // <---- 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/#Float
http://golang.org/pkg/expvar/#NewFloat
Advertisement
Something interesting
Tutorials
+11.5k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+24.5k Golang : Time slice or date sort and reverse sort example
+21.1k Golang : For loop continue,break and range
+8.7k Golang : How to join strings?
+19.3k Golang : Get host name or domain name from IP address
+21.1k Golang : Sort and reverse sort a slice of strings
+11.5k Golang : Change date format to yyyy-mm-dd
+6.7k Golang : Check if password length meet the requirement
+22.2k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+26.1k Mac/Linux and Golang : Fix bind: address already in use error
+5.3k Golang : Get FX sentiment from website example
+8.2k Golang : Find relative luminance or color brightness