Skip to content

Instantly share code, notes, and snippets.

@garukun
Last active October 23, 2015 17:15
Show Gist options
  • Save garukun/f60fd7daeafa29dc7755 to your computer and use it in GitHub Desktop.
Save garukun/f60fd7daeafa29dc7755 to your computer and use it in GitHub Desktop.
Go Status Page Using expvar
package status
import (
"expvar"
"net/http"
"encoding/json"
)
// Status method dumps application variables and their values in JSON format.
func Status(writer http.ResponseWriter, request *http.Request) {
vars := make(map[string]string)
expvar.Do(func(kv expvar.KeyValue) {
vars[kv.Key] = kv.Value.String()
})
// Need error handling and closing request body.
writer.Header().Set("Content-Type", "application/json; charset=UTF-8")
json.NewEncoder(writer).Encode(vars)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment