Golang expvar.Publish() function example
package expvar
Publish declares a named exported variable. This should be called from a package's init function when it creates its Vars. If the name (1st parameter) is already registered then this will log.Panic.
Golang expvar.Publish() function usage example
type UptimeVar struct {
StartTime time.Time
}
func (v *UptimeVar) String() string {
return strconv.FormatFloat(time.Since(v.StartTime).Seconds(), 'f', 2, 64)
}
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
hostname, _ := os.Hostname()
hostnameVar.Set(hostname)
expvar.Publish("uptime", &UptimeVar{time.Now()}) // <-- here. Must be called in init() function
...
}
Reference :
Advertisement
Something interesting
Tutorials
+15.6k Golang : ROT47 (Caesar cipher by 47 characters) example
+16.5k Golang : Execute terminal command to remote machine example
+6.5k Golang : Combine slices of complex numbers and operation example
+11.6k Golang : Surveillance with web camera and OpenCV
+39.2k Golang : How to read CSV file
+4.7k Fix Google Analytics Redundant Hostnames problem
+7.4k Golang : Accessing dataframe-go element by row, column and name example
+12.7k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+8.2k Golang : Add build version and other information in executables
+16.4k Golang : Test floating point numbers not-a-number and infinite example
+14.3k Golang : How to shuffle elements in array or slice?
+18.6k Golang : Generate thumbnails from images