Codezillas
Image for post
Image for post

Building a RESTful API with Go (Part 1)

Step 0 — Setup Your Environment

Step One — Basic Structure

Image for post
Image for post

Step Two — Let’s Make a Model

Image for post
Image for post
Image for post
Image for post

Step Three — Implementing the Router

Image for post
Image for post
go get -u github.com/gorilla/mux
Image for post
Image for post
router := mux.NewRouter()
Image for post
Image for post
http.ListenAndServe(":5000", router)
log.Fatal(http.ListenAndServe(":5000", handler))
Image for post
Image for post
./sushiAPI

Step 4 — Controller Methods

Image for post
Image for post

getRolls()

Image for post
Image for post
rolls = append(rolls, Roll{ID: "1", ImageNumber: "8", Name: "Spicy Tuna Roll", Ingredients: "Tuna, Chili sauce, Nori, Rice"})
Image for post
Image for post
Nice!!!

getRoll()

Image for post
Image for post
for i := 0; i < 3; i++ {
fmt.Println(i)
}
for x > y {
fmt.Println(x)
}
for i, element := range myArr {
fmt.Println(element)
}

createRoll()

Image for post
Image for post

Developer, Lighting Designer, and Cat Fancier.