Quickstart
In this quickstart, we’ll glean insights from code segments and learn how to:
Requirements
- Go 1.13 or above
Installation
To install Gin package, you need to install Go and set your Go workspace first.
- Download and install it:
$ go get -u github.com/gin-gonic/gin
- Import it in your code:
import "github.com/gin-gonic/gin"
- (Optional) Import
net/http
. This is required for example if using constants such ashttp.StatusOK
.
import "net/http"
- Create your project folder and
cd
inside
$ mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_"
- Copy a starting template inside your project
$ curl https://raw.githubusercontent.com/gin-gonic/examples/master/basic/main.go > main.go
- Run your project
$ go run main.go
Getting Started
Unsure how to write and execute Go code? Click here.
First, create a file called example.go
:
# assume the following codes in example.go file
$ touch example.go
Next, put the following code inside of example.go
:
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080
}
And, You can run the code via go run example.go
:
# run example.go and visit 0.0.0.0:8080/ping on browser
$ go run example.go
Last modified April 29, 2021 : update Golang version requirement (#167) (0bda0d4)