Creating an Authentication API with Golang
You need to install Go on your PC or MacOS and set your Go workspace first
- The first need Go installed (Latest Stable version is awesome)
- To get started, Create a new project then, Create a new file called
main.go
and enter the following starter code:
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
- To be able to handle GET and POST requests, we create a
GetMethod
andPostMethod
:
func PostMethod(c *gin.Context) {
fmt.Println("\napi.go 'PostMethod' called")
message := "PostMethod called"
c.JSON(http.StatusOK, message)
}
func GetMethod(c *gin.Context) {
fmt.Println("\napi.go 'GetMethod' called")
message := "GetMethod called"
c.JSON(http.StatusOK, message)
}
- We need to create a
Gin
router for handling all the requests:
func main() {
router := gin.Default()
}
- Following this up, we add in the
GetMethod
andPostMethod
:
func main() {
router := gin.Default()
router.POST("/", PostMethod)
router.GET("/", GetMethod)
}