user2799827
user2799827

Reputation: 1127

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)

Upvotes: 4

Views: 6934

Answers (4)

ettanany
ettanany

Reputation: 19806

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

Upvotes: 2

HeartRunner
HeartRunner

Reputation: 11

export GO111MODULE=on
go mod init
go mod tidy

if not succed, add more command:

go mod download

Upvotes: 0

talkdatatome
talkdatatome

Reputation: 664

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.

Upvotes: 0

Hamza Anis
Hamza Anis

Reputation: 2565

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.

Upvotes: 3

Related Questions