Open laughk opened 4 years ago
:thought_balloon: 実は go modules をしっかり触ったことなかったのでそこも含めて
$ go mod init github.com/laughk/TIL/hello-gin
go: creating new go.mod: module github.com/laughk/TIL/hello-gin
$ go get -u github.com/gin-gonic/gin
go: finding github.com/leodido/go-urn v1.2.0
go: finding github.com/mattn/go-isatty v0.0.11
go: finding github.com/modern-go/concurrent latest
go: finding github.com/modern-go/reflect2 v1.0.1
go: finding github.com/go-playground/universal-translator v0.17.0
go: finding github.com/json-iterator/go v1.1.9
go: finding github.com/go-playground/locales v0.13.0
go: finding golang.org/x/sys v0.0.0-20191026070338-33540a1f6037
go: finding golang.org/x/sys latest
go: finding github.com/stretchr/objx v0.2.0
go: finding gopkg.in/yaml.v2 v2.2.7
go: finding gopkg.in/check.v1 latest
go: finding golang.org/x/tools latest
go: finding gopkg.in/go-playground/validator.v9 v9.31.0
go: finding golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee
go: finding golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898
go: finding golang.org/x/sync latest
go: finding golang.org/x/crypto latest
go: finding golang.org/x/net latest
go: finding golang.org/x/mod v0.2.0
go: finding golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
go: finding golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e
go: finding golang.org/x/xerrors latest
go: finding golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
go: downloading github.com/mattn/go-isatty v0.0.11
go: downloading gopkg.in/yaml.v2 v2.2.7
go: downloading gopkg.in/go-playground/validator.v9 v9.31.0
go: extracting github.com/mattn/go-isatty v0.0.11
go: downloading golang.org/x/sys v0.0.0-20200113162924-86b910548bc1
go: extracting gopkg.in/go-playground/validator.v9 v9.31.0
go: downloading github.com/leodido/go-urn v1.2.0
go: downloading github.com/go-playground/universal-translator v0.17.0
go: extracting gopkg.in/yaml.v2 v2.2.7
go: extracting github.com/go-playground/universal-translator v0.17.0
go: downloading github.com/go-playground/locales v0.13.0
go: extracting github.com/leodido/go-urn v1.2.0
go: extracting golang.org/x/sys v0.0.0-20200113162924-86b910548bc1
go: extracting github.com/go-playground/locales v0.13.0
$ ls -a
./ ../ go.mod go.sum main.go
これでもう大体 OK。 らくちん
https://github.com/gin-gonic/gin#quick-start
$ cat main.go
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
if err := run(); err != nil {
panic(err)
}
}
func run() error {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run()
return nil
}
動かす
$ go run main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /ping --> main.run.func1 (3 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
$ http localhost:8080/ping
HTTP/1.1 200 OK
Content-Length: 19
Content-Type: application/json; charset=utf-8
Date: Tue, 14 Jan 2020 10:50:35 GMT
{
"message": "pong"
}
[GIN] 2020/01/14 - 19:50:30 | 404 | 1.681µs | ::1 | GET /
[GIN] 2020/01/14 - 19:50:35 | 200 | 52.465µs | ::1 | GET /ping
めっちゃ簡単だしわかりやすい :bulb:
:thought_balloon: Gin の README を眺めているけど、長めではあるもののいい感じに簡単なサンプルコードが大量にあってかなりとっつきやすさがあるなー
そういや Gin 自体は特に DB を扱う機構はない感じなのね。
業務だと go-swagger を触っているのだけど、有名所のフレームワークをざざっと触って感覚を掴むみたいなことをやっておきたいので触ってみる。
ちなみに今回は Goもくもく会(ごもくかい)#23 - connpass にお邪魔している