YouTube Video:
How to start with gin framework
Hello friends! In this post, I will be discussing about how to start with Gin framework. First of all, you have to install the latest version of Golang. Now, create a folder myApp and open it your favourite IDE. I am using VS Code. Enter into the folder in the terminal. You can use below command to enter into a folder if needed:
cd <Folder_Name>
Now, run below command to initialise the module:
go mod init <Module_Name>
Now, create a file main.go and import the below package inside it.
"github.com/gin-gonic/gin"
Till now, you main.go file should look like below:
package main
Now, you can run below command:
go mod tidy
Now, we will define controller functions:
Here, You can notice one thing is, we are using an input of *gin.Context in each case. It is a mandatory input in controller function in Gin application.
Now, we will define routers. We will define them inside main function before calling the Run() function.
The whole code is like below:
That's it guys! We are done. You can simply run below command to start the application.
go run main.go
Now, you can go to your favourite browser and hit the below urls to see the results:
localhost:8081
localhost:8081/about
localhost:8081/contact
YouTube Video: