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
+16.2k Golang : How to remove certain lines from a file
+4.3k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+7.8k Golang : GMail API create and send draft with simple upload attachment example
+5.2k Golang : Join lines with certain suffix symbol example
+16.1k Golang : Qt image viewer example
+5k Golang : Function as an argument type example
+6.2k Golang : Example of custom handler for Gorilla's Path usage.
+4k Fix Google Analytics Redundant Hostnames problem
+5.1k Golang : Dealing with backquote
+8.9k Golang : Setting variable value with ldflags
+13.2k Golang : GUI with Qt and OpenCV to capture image from camera
+15.1k Golang : Check if a string contains multiple sub-strings in []string?