Created
June 13, 2017 22:18
-
-
Save colynb/4346c4600f0ca5afe421ef0d708ebbc6 to your computer and use it in GitHub Desktop.
Stupid JSON server in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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