Basic rest – first plunge

B

What we got

Hopefully, with Go installed and an IDE ready for tapping, we can begin. If you have a friendly IDE, you’ll want to start a mod-enabled project and name it something worthy of our work. If not, then within the command line, we can initialise a new mod project.

go init mod

This should initialise the go.mod file and look something like this.

module BasicGoREST

go 1.21

Now, to make our lives just a little easier, we’re going to add a web framework to give it a little flavour. According to go-web-framework-stars Gin is the top-starred framework, so let’s trust the masses and follow.

If you check out gin it has some how-to instructions on installing. We can do this with

go get -u github.com/gin-gonic/gin

Now with it being installed you should see some dependencies in the go.mod file and we can get on to the good stuff, the cream of this blog post.

Act 2 – fresh paper, new ink

Now to start we might as well rob what is on the gin read me and create a main.go file in the root and paste that code.

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": "pong",
		})
	})
	r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

If we run that and use whatever tool you have (Postman, Curl, etc) we should get back a 200 with a message containing pong.

We now now in the starting position to start laying down the track for IH Corp business needs. Let’s start with a couple of endpoints to test a model and get that initial rush of a plan coming together, shall we?

If we look back we can see the endpoint for CreateProduct and GetProduct with these two we successfully start adding products to our “eventual” database and retrieve it.

  • Product Details: GET /products/{id} to retrieve details of a specific inflatable hero.
  • Create Product: POST /products to add a new inflatable hero to the catalog.

Defining these endpoints

r.POST("/products", func(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"message": "created",
	})
})
r.GET("/products/:id", func(c *gin.Context) {
	id := c.Param("id")
	c.JSON(http.StatusOK, gin.H{
		"message": fmt.Sprintf("found %s", id),
	})
})

You can see in the code example, we set the two endpoints. For this GET, we define a variable name :id that allows us to tell Gin we plan to access this part of the route and use it. You can see this with the id := c.Param("id") line. We access the value with a function that returns the value of the param by key.

If we test those endpoints, in the terminal while running the main file (go run main.go), we should see some logs.

[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
[GIN] 2023/12/04 - 13:21:25 | 200 |      62.457µs |             ::1 | GET      "/products/1"
[GIN] 2023/12/04 - 13:21:49 | 200 |      15.021µs |             ::1 | POST     "/products"

If you do, awesome! We wrote some Go, did some endpoints, and got some damn results. Look at us! Killing it.

This is a good place to take a breath, sip the coffee, and think about how we’re going to define our models and connect to a database.

Thanks a bunch,
Ryan

About the author

admin-blog

Add comment

By admin-blog

Recent Comments

No comments to show.