Skip to content

Instantly share code, notes, and snippets.

@colynb
Created June 13, 2017 22:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colynb/4346c4600f0ca5afe421ef0d708ebbc6 to your computer and use it in GitHub Desktop.
Save colynb/4346c4600f0ca5afe421ef0d708ebbc6 to your computer and use it in GitHub Desktop.
Stupid JSON server in golang
package main
import (
"net/http"
"encoding/json"
)
type Person struct {
ID int `json:"id"`
Firstname string `json:"firstname"`
}
var people []Person
func main() {
people = append(people, Person{ID: 1, Firstname:"Colyn"})
people = append(people, Person{ID: 2, Firstname:"April"})
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(people)
})
http.ListenAndServe(":8000", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment