Golang is a programming language developed by Google. It can used to create cloud services, web , CLI tools etc. The first step to use Go is download and install the SDK.
Basic setup
To build basic API in Golang we need two packages
- http
- gin
go get github.com/gin-gonic/gin
Gin help us to build API routes . In the main.go file add the following
package main
import
("github.com/gin-gonic/gin"
"net/http")
func index(c *gin.Context){
c.JSON(200,gin.H{"message": "Welcome to Golang API"})
}
func main(){
r:=gin.Default()
r.GET("/",index)
r.Run()
}
Here we create a gin instance which can be used to create API route. We also define handler function for the API too.
:= is the short hand in Golang for creating variables
Run the project
By using go run . or go run main.go can execute the program.
- How to add packages in Golang
- Create REST API using gin in Golang
- How to Containerize Golang app with Docker
- How to create API using mux in Golang
- How to create API using gin in Golang
- How to create API using Fiber in Golang
- Set up golang project
- How to use GORM packages in Golang
- How to solve gcc error in Golang -Windows