hello.go 482 B

12345678910111213141516171819202122232425
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "runtime"
  6. "strconv"
  7. )
  8. type MessageStruct struct {
  9. Message string
  10. }
  11. func hello(w http.ResponseWriter, r *http.Request) {
  12. w.Header().Set("Content-Type", "application/javascript")
  13. j, _ := json.Marshal(&MessageStruct{"Hello, world"})
  14. w.Header().Set("Content-Length", strconv.Itoa(len(j)))
  15. w.Write(j)
  16. }
  17. func main() {
  18. runtime.GOMAXPROCS(runtime.NumCPU())
  19. http.HandleFunc("/json", hello)
  20. http.ListenAndServe(":8080", nil)
  21. }