Skip to content

Instantly share code, notes, and snippets.

@kgoralski
Created November 14, 2016 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgoralski/e197863f6b201a9790914e5158cdfbbc to your computer and use it in GitHub Desktop.
Save kgoralski/e197863f6b201a9790914e5158cdfbbc to your computer and use it in GitHub Desktop.
basic_rest_server
package main
import (
"encoding/json"
"log"
"net/http"
)
type Person struct {
ID int `json:"id"`
Name string `json:"name"`
}
type People []Person
func getPersonByIDHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
people := People{
Person{ID: 1, Name: "Janusz"},
Person{ID: 2, Name: "Bożena"},
}
if err := json.NewEncoder(w).Encode(people); err != nil {
http.Error(w, "something went wrong", http.StatusInternalServerError)
}
}
func main() {
startServer()
}
func startServer() {
http.HandleFunc("/rest/people/", getPersonByIDHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment