My go version go version go1.12.7 darwin/amd641
I installed echo using go get gopkg.in/labstack/echo.v4
Its should build.
Unable to build the project.
Install echo go get gopkg.in/labstack/echo.v4 as said in the line https://godoc.org/gopkg.in/labstack/echo.v4
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)
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:
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)go.mod file which is just a very simple text file.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
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:
So, you can still follow the echo README example code:
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)go.modfile which is just a very simple text file.go buildthis 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