goコマンド『get』

Go言語に組み込まれているコマンド『get』についてまとめました。

動作環境

ホストOSWindows 10 21H1(19043.2130) 64bit
WSL2
Visual Studio Code1.70.2
Docker Desktop4.12.0(85629)
Go1.19.2

構文

# modファイルが用意された状態で実行する
# ソースコード上でimportされている外部パッケージをmodファイルに追加する

go get github.com/labstack/echo/v4
go get github.com/labstack/echo/v4/middleware

サンプル

.
    ├── go.mod
    └── main.go
// 実行後のgo.modの内容
module sample

go 1.19

require github.com/labstack/echo/v4 v4.9.1

require (
	github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
	github.com/labstack/gommon v0.4.0 // indirect
	github.com/mattn/go-colorable v0.1.13 // indirect
	github.com/mattn/go-isatty v0.0.16 // indirect
	github.com/valyala/bytebufferpool v1.0.0 // indirect
	github.com/valyala/fasttemplate v1.2.2 // indirect
	golang.org/x/crypto v0.1.0 // indirect
	golang.org/x/net v0.1.0 // indirect
	golang.org/x/sys v0.1.0 // indirect
	golang.org/x/text v0.4.0 // indirect
	golang.org/x/time v0.1.0 // indirect
)
// main.goの内容
package main

import (
	"net/http"

	"github.com/labstack/echo/v4"
	"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, "こんにちわ, 世界!")
}
go build
./[ビルドされたファイル名]

   ____    __
  / __/___/ /  ___
 / _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.9.0
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
                                    O\
⇨ http server started on [::]:1323

ブラウザで表示してみる