Golang Gin-Gonic RESTful API Simple Tutorial (no db)

Assuming you’ve got gin-gonic setup using go get GitHub.com/gin-gonic/gin This tutorial will cover GET and POST, using example of sending username and password, let’s start!

Our struct

the User struct, Username and Password is capitalized, meaning it’ll be public accessible, as for json we’ll use regular javascript convention

I like to keep things descriptive, so our main is quite simple. So let’s take a look at our routes. By default, it’ll run on localhost:8080


We have to setupOPTIONS because sometimes request will ask for it. If you don’t have it, POST might not work. read more

GET Method

when we receive a get request, we’ll just print it out, here’s what it looks like

POST Method

Here we check for methods, if request is options, we send the appropriate headers. (note, you might need a chrome extension called Allow-Control-Allow-Origin:* by vitvad to allow CORS actions.

Now for POST we create a variable u with struct type User


Above line assign the JSON we got from gin’s context (to u’s pointer).

c.JSON(http.StatusOK, gin.H{ ... })

then we just send back a json with the information we got from user. That’s it. I hope this should simple enough to get you start with GO and GIN API. Here’s the complete code

btw, if you want to test your code, here’s the JS (html) code