简体   繁体   中英

Can't seem to get started with Go and Echo

Trying to build a simple crud api with Golang and Echo and I can't get past the first tep in the Echo docs.

I run go get -u github.com/labstack/echo/...

then I create server.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, "Hello, World!")
    })
    e.Logger.Fatal(e.Start(":1323"))
}

but when I try to run: go run server.go

I get this error:

server.go:6:2: cannot find package "github.com/labstack/echo/v4" in any of:
    /usr/local/Cellar/go/1.14.4/libexec/src/github.com/labstack/echo/v4 (from $GOROOT)
    /Users/dariusgoore/go/src/github.com/labstack/echo/v4 (from $GOPATH)

You need to enable GO111MODULE . To enable the module you need to run this command.

export GO111MODULE=on

After enabling it when you will run go run server.go then it will install the packages again after that the program will run as expected.

I get the same issue when I run go get before go mod init . Using the following commands, I can run the server successfully:

go mod init example.com/try-echo
go get
go run server.go

I just created a new project with the same main.go and it ran without any issue. I have listed the steps I followed.

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, "Hello, World!")
    })
    e.Logger.Fatal(e.Start(":1323"))
}

and used go.mod for dependencies downloads

go mod init

go get

go run main.go

and it ran without any error

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

I hope this helps. If it doesn't you can run go env and share the result of the command, which will help to debug further.

export GO111MODULE=on
go mod init
go mod tidy

if not succed, add more command:

go mod download

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM