Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to install echo #1374

Closed
shanmugharajk opened this issue Jul 30, 2019 · 7 comments
Closed

Unable to install echo #1374

shanmugharajk opened this issue Jul 30, 2019 · 7 comments

Comments

@shanmugharajk
Copy link

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)

@ghost
Copy link

ghost commented Jul 30, 2019

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.

@ghost
Copy link

ghost commented Jul 30, 2019

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

@shanmugharajk
Copy link
Author

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

@ghost
Copy link

ghost commented Jul 31, 2019

@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

@vishr
Copy link
Member

vishr commented Aug 1, 2019

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

@vishr vishr closed this as completed Aug 1, 2019
@realuser
Copy link

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"??

@realuser
Copy link

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

lammel added a commit to labstack/echox that referenced this issue Feb 4, 2021
* Improve installation section to avoid errors like `https://github.com/labstack/echo/issues/1374`

Co-authored-by: Roland Lammel <roland@lammel.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants