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