Golang expvar.Int type, NewInt(), Add(), Set() and String() functions examples
package expvar
Int is a 64-bit integer variable that satisfies the Var interface.
Example 1 :
var (
adminPort = flag.String("adminPort", "4567", "localhost port to boot the admin server on")
apiVars = expvar.NewMap("api")
staticVars = expvar.NewMap("static")
webVars = expvar.NewMap("web")
apiRequests = new(expvar.Int) // <-- here
staticRequests = new(expvar.Int)
)
Example 2 :
package main
import (
"fmt"
"net/http"
"expvar"
)
func kvfunc(kv expvar.KeyValue) {
fmt.Println(kv.Key, kv.Value)
}
func main() {
var inerInt int64 = 10 /// <---- here
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) // <---- 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 :
Advertisement
Something interesting
Tutorials
+10.6k Golang : Resolve domain name to IP4 and IP6 addresses.
+4.7k Fix Google Analytics Redundant Hostnames problem
+12.1k Golang : md5 hash of a string
+9.9k Golang : Function wrapper that takes arguments and return result example
+19k Golang : Padding data for encryption and un-padding data for decryption
+21.5k Golang : How to read float value from standard input ?
+9.4k Golang : How to protect your source code from client, hosting company or hacker?
+11.2k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+5.4k Golang : What is StructTag and how to get StructTag's value?
+19.3k Golang : Get host name or domain name from IP address
+18k Golang : Get all upper case or lower case characters from string example
+3.7k Java : Random alphabets, alpha-numeric or numbers only string generator