Echo: Unable to install echo

Created on 30 Jul 2019  路  7Comments  路  Source: labstack/echo

Issue Description

My go version go version go1.12.7 darwin/amd641

I installed echo using go get gopkg.in/labstack/echo.v4

Expected behaviour

Its should build.

Actual behaviour

Unable to build the project.

Steps to reproduce

  1. Install echo go get gopkg.in/labstack/echo.v4 as said in the line https://godoc.org/gopkg.in/labstack/echo.v4

  2. build with the following code

package main

import (
    "net/http"

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

// Handler
func hello(c echo.Context) error {
    return c.String(http.StatusOK, "Hello, World!")
}

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"))
}

Got the following error

main.go:6:2: cannot find package "github.com/labstack/echo/v4" in any of:
/usr/local/go/src/github.com/labstack/echo/v4 (from $GOROOT)
/Users/.../Code/.../Go/src/github.com/labstack/echo/v4 (from $GOPATH)

main.go:7:2: cannot find package "github.com/labstack/echo/v4/middleware" in any of:
/usr/local/go/src/github.com/labstack/echo/v4/middleware (from $GOROOT)
/Users/.../Code/.../Go/src/github.com/labstack/echo/v4/middleware (from $GOPATH)

Most helpful comment

Figured it out. As initially mentioned by @ShrewdSpirit, _your_ code also needs to be in a module.
As a relative go newbie I was following https://golang.org/doc/code.html which talks about workspaces but now it seems there is an entirely different way to organize your code via modules.

You can read about it here:
https://blog.golang.org/using-go-modules

Note:

"As of Go 1.11, the go command enables the use of modules when the current directory or any parent directory has a go.mod, provided the directory is outside $GOPATH/src. (Inside $GOPATH/src, for compatibility, the go command still runs in the old GOPATH mode, even if a go.mod is found. See the go command documentation for details.) Starting in Go 1.13, module mode will be the default for all development."

So, you can still follow the echo README example code:

  • But make sure your project folder is _OUTSIDE_ of the $GOPATH/src directory structure
  • Make sure your GOPATH environment variable is set
  • still use "github.com/labstack/echo/v4" in your import
  • in your project directory, run go mod init example.com/main (example.com can be whatever namespace you want and main is your module name which can be anything as well)
  • this will simply create a go.mod file which is just a very simple text file.
  • run go build this will download echo and its dependencies to your workspace folder. Also it will automatically update your go.mod file to include the echo lab dependency

All 7 comments

Check that path, I installed it yesterday and i just imported it like this:

import "github.com/labstack/echo"
import "github.com/labstack/echo/middleware"

make sure all dependencies are installed.

Having the same issue here. @dmatuteb solution doesn't work. Getting the package go get -u github.com/labstack/echo/... gets version 3 and it has conflict and intellisense errors

I used go get gopkg.in/labstack/echo.v4 and I am able to import this way

import "github.com/labstack/echo"
import "github.com/labstack/echo/middleware"

and its working fine. But the moment when I include this line e.Use(middleware.CORS) the vs code intellisense gives the following error

cannot use middleware.CORS (value of type func() invalid type) as echo.MiddlewareFunc value in argument to e.UseLSP

even though the code is compiling and working.

This is my entire code,

package main

import (
    "fmt"
    "time"

    "github.com/labstack/echo/middleware"

    "github.com/labstack/echo"
)

func delayMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
    return func(c echo.Context) error {
        // TODO: Check how does this work whether this entirely blocking the goroutine or not?
        time.Sleep(2 * time.Second)
        return next(c)
    }
}

func cors(next echo.HandlerFunc) echo.HandlerFunc {
    return func(c echo.Context) error {
        c.Response().Header().Set("Access-Control-Allow-Origin", "*")
        return next(c)
    }
}

func main() {
    e := echo.New()

    e.Use(cors)

    e.Use(middleware.CORS)

    e.GET("/", func(c echo.Context) error {
        return c.String(200, "Welcome to image server")
    })

    e.GET("/images-delayed/:id", func(c echo.Context) error {
        fileName := c.Param("id")
        return c.File(fmt.Sprintf("./images/%s", fileName))
    }, delayMiddleware)

    e.GET("/images/:id", func(c echo.Context) error {
        fileName := c.Param("id")
        return c.File(fmt.Sprintf("./images/%s", fileName))
    })

    e.Logger.Fatal(e.Start(":4000"))
}

@shanmugharajk I had the same issue and found no solution. Changed my package to a go module and added github.com/labstack/echo/v4 v4.1.6 to require section of my go.mod file and ran go get -u. Everything works fine now

Echo v4 is suppose to work with Go modules like @ShrewdSpirit mentioned. We still need to update the docs.

I have this same issue. I'm not clear on what the solution is from this thread.

go version go1.13.1 windows/amd64

C:\dev\go\src\username\test>go get github.com/labstack/echo/v4
package github.com/labstack/echo/v4: cannot find
package "github.com/labstack/echo/v4" in any of:
        c:\go\src\github.com\labstack\echo\v4 (from $GOROOT)
        C:\dev\go\src\github.com\labstack\echo\v4 (from $GOPATH)

Despite this error, it did seem to download the source to C:\dev\go\src\github.com\labstack\echo

Should we use import "github.com/labstack/echo" instead of import "github.com/labstack/echo/v4"??

Figured it out. As initially mentioned by @ShrewdSpirit, _your_ code also needs to be in a module.
As a relative go newbie I was following https://golang.org/doc/code.html which talks about workspaces but now it seems there is an entirely different way to organize your code via modules.

You can read about it here:
https://blog.golang.org/using-go-modules

Note:

"As of Go 1.11, the go command enables the use of modules when the current directory or any parent directory has a go.mod, provided the directory is outside $GOPATH/src. (Inside $GOPATH/src, for compatibility, the go command still runs in the old GOPATH mode, even if a go.mod is found. See the go command documentation for details.) Starting in Go 1.13, module mode will be the default for all development."

So, you can still follow the echo README example code:

  • But make sure your project folder is _OUTSIDE_ of the $GOPATH/src directory structure
  • Make sure your GOPATH environment variable is set
  • still use "github.com/labstack/echo/v4" in your import
  • in your project directory, run go mod init example.com/main (example.com can be whatever namespace you want and main is your module name which can be anything as well)
  • this will simply create a go.mod file which is just a very simple text file.
  • run go build this will download echo and its dependencies to your workspace folder. Also it will automatically update your go.mod file to include the echo lab dependency
Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexzorin picture alexzorin  路  3Comments

arun0009 picture arun0009  路  3Comments

dre1080 picture dre1080  路  4Comments

linux-support picture linux-support  路  3Comments

absinsekt picture absinsekt  路  4Comments