The shell of an go app
package main
import (
//automatique
)
var books []Book
// Get All Books
func getBooks(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(books)
}
// Book Structs (Model) // Like a class
type Book struct {
ID string `json:"id"`
Isbn string `json:"isbn"`
Title string `json:"title"`
Author *Author `json:"author"`
}
// Author struct
type Author struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
func main() {
// Init Router
r := mux.NewRouter()
// Mock Data - @todo - implement DB
books = append(books, Book{ID: "1", Isbn: "448743", Title: "Book One", Author: &Author{Firstname: "John", Lastname: "Doe"}})
books = append(books, Book{ID: "2", Isbn: "855338", Title: "Book Two", Author: &Author{Firstname: "Treave", Lastname: "Smith"}})
// Router Handlers / Endpoints
r.HandleFunc("/api/books", getBooks).Methods("GET")
log.Fatal(http.ListenAndServe(":8000", r))
}