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.

  1. Download and install it:
  1. $ go get -u github.com/gin-gonic/gin
  1. Import it in your code:
  1. import "github.com/gin-gonic/gin"
  1. (Optional) Import net/http. This is required for example if using constants such as http.StatusOK.
  1. import "net/http"
  1. Create your project folder and cd inside
  1. $ mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_"
  1. Copy a starting template inside your project
  1. $ curl https://raw.githubusercontent.com/gin-gonic/examples/master/basic/main.go > main.go
  1. Run your project
  1. $ go run main.go

Getting Started

Unsure how to write and execute Go code? Click here.

First, create a file called example.go:

  1. # assume the following codes in example.go file
  2. $ touch example.go

Next, put the following code inside of example.go:

  1. package main
  2. import "github.com/gin-gonic/gin"
  3. func main() {
  4. r := gin.Default()
  5. r.GET("/ping", func(c *gin.Context) {
  6. c.JSON(200, gin.H{
  7. "message": "pong",
  8. })
  9. })
  10. r.Run() // listen and serve on 0.0.0.0:8080
  11. }

And, You can run the code via go run example.go:

  1. # run example.go and visit 0.0.0.0:8080/ping on browser
  2. $ go run example.go

Last modified April 29, 2021 : update Golang version requirement (#167) (0bda0d4)