Golang – Gin Get Started

スポンサーリンク

Gin

Gin is a web framework written in Go (Golang). It features a martini-like API with performance that is up to 40 times faster thanks to httprouter. If you need performance and good productivity, you will love Gin. (From Gin web site https://github.com/gin-gonic/gin)

Preparation

Use Gin is very simple, let’s create simple project and try hello world.

  1. Create a folder under ${GOPATH}/src
  2. Go to above folder
  3. Run go mod init to generate go.mod file
  4. Install gin using go get -u github.com/gin/gonic/gin
cd ${GOPATH}/src
mkdir ginexample
cd ginexample
go mod init
go get -u github.com/gin-gonic/gin

Write simple codes

Let’s write simple codes just return hello world!

Create main.go under above go folder

package main
import (
    "log"
    "net/http"
    "github.com/gin-gonic/gin"
)
func main() {
    router := gin.Default()
    router.GET("/", func(c *gin.Context) {
        c.String(http.StatusOK, "Hello World!")
    })
    log.Fatal(router.Run(":8080"))
}

It’s simple codes. This run internal server with 8080 port.

Run

Run above codes with following command.

go run main.go

Access http://localhost:8080 by browser

You can see Hello World! on the browser

未分類
スポンサーリンク
Professional Programmer2

コメント