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
+10.9k Golang : Natural string sorting example
+7.6k Golang : Dealing with struct's private part
+15.3k Golang : Get timezone offset from date or timestamp
+9k Golang : Populate or initialize struct with values example
+10.7k Golang : Get local time and equivalent time in different time zone
+11.9k Golang : convert(cast) float to string
+6.4k Golang : How to get capacity of a slice or array?
+7.6k Golang : How to stop user from directly running an executable file?
+10.5k Golang : cannot assign type int to value (type uint8) in range error
+13.2k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+8.1k Findstr command the Grep equivalent for Windows
+18.5k Golang : How to remove certain lines from a file