Building Gin environment
On this page
overview
To use Gin, you need to install Go, so please install that.
To install, select the installation method that suits your environment from the official website. https://go.dev/doc/install
create gin environment
- Create go.mod for module management. Create a working directory and run go mod init
mkdir gin
cd gin
go mod init hello
- To use Gin, install Gin using GoCLI.
go get -u github.com/gin-gonic/gin
- Let’s actually write the code
vi main.go
main.go
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/hello", func(c *gin.Context) {
c.JSON(200, gin.H{ "message": "hello" })
})
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
The final file structure is as follows.
execution
After creating the environment, run the following command to check if it can be started.
go run main.go
If the command succeeds, Please access http://localhost:8080/hello.
You should see the following.
summary
I think it was very easy. Next time, we will use this environment to create routing.
Also, if you create an environment with Docker, the following article will be helpful. [Create a Go (Gin) environment with Docker]