LoginSignup
0
0

More than 1 year has passed since last update.

【Go】echoを使ってAPIサーバを構築する際に起きたエラーの備忘録

Last updated at Posted at 2021-06-16

About

windows環境でVS Codeを使用。Goのechoを用いてApiサーバの構築のチュートリアルを行おうとした。

ApiTest.go
package main

import (
    "net/http"

    "github.com/labstack/echo/v4"
)

func main() {
    e := echo.New()
    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "こんにちは!")
    })
    e.Logger.Fatal(e.Start(":1323"))
}

どんなエラーが起きたか

package github.com/labstack/echo/v4: cannot find package "github.com/labstack/echo/v4" in any of:
        /usr/local/go/src/github.com/labstack/echo/v4 (from $GOROOT)
        /home/username/go/src/github.com/labstack/echo/v4 (from $GOPATH)

みたいなことを言われたので色々調べてみるとどうやら先にgo.madとやらを手に入れないといけない模様。

go mod init echo-sample

上記を実行してファイルを入手すると実行ファイルと同じファイル内に作成される。

これでもう一度実行すると

C:\Users\Documents\GoApp>go get github.com/labstack/echo/v4
go get: added github.com/labstack/echo/v4 v4.3.0

となり、go.madファイルを見ると

go.mad
module echo-sample

go 1.16

require github.com/labstack/echo/v4 v4.3.0 // indirect

GitHubの部分が追加されているのでこれでOKっぽい。
実行するとしっかり動いてくれた。
公式GitHubを読んでもよくわからなかったので同じような状況に陥った方がいれば...。

また何分初学者のため指摘やご教授していただければなと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0