Skip to content

Instantly share code, notes, and snippets.

@kgoralski kgoralski/basic_rest_server.go Secret
Created Nov 14, 2016

What would you like to do?
package main
import (
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() {
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
You can’t perform that action at this time.