4

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)
user2799827
  • 1,077
  • 3
  • 18
  • 54
  • 1
    Run `go get github.com/labstack/echo/v4` as described in [the installation instructions](https://github.com/labstack/echo#guide). – Charlie Tumahai Jun 26 '20 at 22:14
  • 1
    Returns the same error message: 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) – user2799827 Jun 26 '20 at 22:23
  • 1
    @user2799827 enable modules. See [How to Write Go Code](https://golang.org/doc/code.html) if you are new to writing Go code. – Charlie Tumahai Jun 26 '20 at 22:33

4 Answers4

2

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.

Hamza Anis
  • 2,475
  • 1
  • 26
  • 36
2

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
ettanany
  • 19,038
  • 9
  • 47
  • 63
0

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.

talkdatatome
  • 614
  • 1
  • 10
  • 17
0
export GO111MODULE=on
go mod init
go mod tidy

if not succed, add more command:

go mod download