Starting with Gin in Golang

Starting with Gin in Golang

Gin is a web framework written in Golang. It is very popular framework as it is very easy to start with it and performance is very good. In this article, I will let you know how you can run your first gin application with very few steps.

Step 1: create a directory with name of your choice for example: gin-hello-world

Step 2: Open this directory in VSCode. I am using VSCode, you can use any other code editor of your choice. Now, open terminal and run below command to create go module.
go mod init gin-hello-world

Step 3: Create a file main.go in this directory and paste below code.

package main

import (
        "net/http"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "hello world")
	})
	r.Run()
}

Step 4: You need to get gin framework from repository to run this code. So use command to get it (in terminal).
go get github.com/gin-gonic/gin

Step 5: Now you are ready to run your first go code with gin framework. Use command go run main.go to run your first application.

This will start a local server at your machine at port 8080. To test your application, open browser and type localhost:8080. you will get below output at your browser.

Leave a Reply

Your email address will not be published. Required fields are marked *