monorepo# STEP 1
cd apps
mkdir api-go
cd api-go
# STEP 2 : Initialising empty go backend
go mod init github.com/RitikaxG/runState/apps/api-go
apps/api-gocmd/server
|-------------> main.go
internals
|----> routes
|-------> websites.go
|-------> health.go
|-------> router.go
Let’s say you want to use the popular web framework Gin.
You run:
go get github.com/gin-gonic/gin
This does:
go.mod with the dependency + version.go.sum file (checksum file to ensure dependency integrity).Example go.mod after installing Gin:
module github.com/hkirat/go-http-server
go 1.22
require github.com/gin-gonic/gin v1.9.1
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default() // creates Gin Router with default middleware
// Defines a simple GET endpoint
r.GET("/health", func(c *gin.Context) {
// Returns JSON response
c.JSON(200, gin.H{ // 200 can be replaced with http.StatusOK this way u use net/http with Gin for status codes
"status": "ok",
})
})
// Start server at 30
r.Run(":3001")
}