Serve JSON with Go HTTP

Create a simple JSON status endpoint using Go's HTTP standard library.

Original Pearto instructional example. Test it in your own environment and adapt it to your requirements.

News & updates

No news stories have been linked to this profile.

Search news coverage ↗

Sources & useful links

No publisher or independent source links have been added to this entry.

Suggest a source or correction ↗
package main
import (
  "encoding/json"
  "log"
  "net/http"
)
func main() {
  http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(map[string]bool{"ok": true})
  })
  log.Fatal(http.ListenAndServe(":8080", nil))
}