次はECHOフレームワークに触れてみる
とりあえずgoのバージョン確認
$ go version go version go1.18.4 linux/amd64
もしくは
package main import ( "fmt" "runtime" ) func main() { fmt.Println("Go version :", runtime.Version(), ", OS :", runtime.GOOS, ", ARCH :", runtime.GOARCH) }
こんなかんじ
V1.18だとGO111MODULEはデフォルトでONのはず
なので、そのままHello worldを作ってみる。
$ mkdir tutorial_2 && cd tutorial_2 $ go mod init tutorial_2 go: creating new go.mod: module tutorial_2 $ go get github.com/labstack/echo/v4
package main import ( "net/http" "github.com/labstack/echo/v4" // <- Echo v4をimportしている "github.com/labstack/echo/v4/middleware" ) func main() { // Echo instance e := echo.New() // Middleware e.Use(middleware.Logger()) e.Use(middleware.Recover()) // Routes e.GET("/", hello) // Start server e.Logger.Fatal(e.Start(":1323")) } // Handler func hello(c echo.Context) error { return c.String(http.StatusOK, "Hello, World!") }
んじゃビルドっておもったところ以下のようにエラーが出る。HelloWorldで躓くとは...
$ go build ../../go/pkg/mod/github.com/labstack/echo/v4@v4.9.1/middleware/jwt.go:9:2: missing go.sum entry for module providing package github.com/golang-jwt/jwt (imported by github.com/labstack/echo/v4/middleware); to add: go get github.com/labstack/echo/v4/middleware@v4.9.1 ../../go/pkg/mod/github.com/labstack/echo/v4@v4.9.1/middleware/rate_limiter.go:9:2: missing go.sum entry for module providing package golang.org/x/time/rate (imported by github.com/labstack/echo/v4/middleware); to add: go get github.com/labstack/echo/v4/middleware@v4.9.1
とりあえずGoogle先生に相談したらgo.sumに乗っていないのが原因らしい。
というわけで、もう一度やり直し。
$ go mod tidy go: downloading github.com/stretchr/testify v1.7.0 go: downloading github.com/golang-jwt/jwt v3.2.2+incompatible go: downloading golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 go: downloading github.com/davecgh/go-spew v1.1.1 go: downloading github.com/pmezard/go-difflib v1.0.0 go: downloading gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b $ go build
今度は問題なくビルドが通ったので実行してみる。
./tutorial_2 ____ __ / __/___/ / ___ / _// __/ _ \/ _ \ /___/\__/_//_/\___/ v4.9.0 High performance, minimalist Go web framework https://echo.labstack.com ____________________________________O/_______ O\ ? http server started on [::]:1323 {"time":"2022-11-02T13:38:57.697627938+09:00","id":"","remote_ip":"::1","host":"localhost:1323","method":"GET","uri":"/","user_agent":"curl/7.61.1","status":200,"error":"","latency":6512,"latency_human":"6.512μs","bytes_in":0,"bytes_out":13}
$ curl "http://localhost:1323" Hello, World!
これでとりあえずEchoが動作した。よかったよかった。
参考
Goの1.16で missing go.sum entry for module ... to add: ...と出るときの対処法 - Qiita