Golang web (2)- Gin framework

Jane
Apr 24, 2021

--

既上一篇的net/http內建的方法外

golang有很多其他的web framework

其中最熱門的最快速的就是Gin這個framework

GitHub link ->https://github.com/gin-gonic/gin

Document link ->https://gin-gonic.com

What is Gin?

Gin is a web framework written in Golang.

It features a Martini-like API, but with performance up to 40 times faster than Martini.

If you need performance and productivity, you will love Gin.

HelloWorld of Gin Framework

首先~要裝gin

$ go get -u github.com/gin-gonic/gin

並在script import

import "github.com/gin-gonic/gin"

hello world example

gin.default()建立一個route

route定義了在”/” path下以HTTP GET method回覆context

http status = 200

http印出hello world

route.Run沒有指定port的話就是預設8080port

package mainimport (
"github.com/gin-gonic/gin"
)
func main() {
route := gin.Default()
route.GET("/", func(c *gin.Context) {
c.String(200, "hello world")
})
//default port:8080
route.Run()
}

使用curl

$ curl -X GET localhost:8080

Gin default會把所有request log print出來

--

--

Jane
Jane

Written by Jane

Life’s a Struggle But You Can Win

No responses yet