dongyilu3143 2016-03-27 10:24
浏览 30
已采纳

前往:如何在不重命名的情况下处理包裹冲突?

I'm trying to solve a package collision without renaming, as i don't think it's particularly elegant. So at the moment I have my own middleware which just handles database connections, but I also use middleware from Echo. Echo uses middleware as their middleware's package name too.

So the solution i'm trying to implement is one in which would extend Echo's package. But i'm not having any success and have not found any info on doing this.

See echo middleware here: https://github.com/labstack/echo/tree/master/middleware

main.go

package main


import (
    "github.com/facebookgo/grace/gracehttp"
    "github.com/labstack/echo"
    "github.com/labstack/echo/engine/standard"
    "gitlab.com/project/middleware" //This is the middleware repository
    "github.com/asaskevich/govalidator"
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
    "encoding/json"
    "log"
)


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

    e.Use(middleware.Db()) //Custom middleware
    e.Use(middleware.Logger()) //Echo middleware
    e.Use(middleware.Recover()) //Echo middleware

    //Compile fails because Logger and Recover are not being exported.

    e.Post("/", createUser())
    e.Get("/", getUser())
    e.Put("/", updateUser())
    e.Delete("/", removeUser())

    s := standard.New(":3000")
    s.SetHandler(e)
    gracehttp.Serve(s.Server)
}

Middleware structure

├── middleware
│   ├── db.go //Custom middleware
│   └── echo.go //Echo middleware

So db.go and echo.go are both packaged as middleware, but I'm not able to 'export' the functions from the Echo package which is imported.

echo.go

package middleware

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

//Stop compiler errors
//Echo middleware is usually accessed with middleware.Logger
//But by using the preceding dot, you can drop the prefix
var _ = Logger()

GO's compiling fails because Logger() and Recover() function are not exported despite being capitalised - whereas Db() from the db.go package is exported.

  • 写回答

1条回答 默认 最新

  • duancai7002 2016-03-27 11:19
    关注

    Your import solution doesn't work because even if the echo middleware package is imported without identifier, its exported identified aren't part of your middleware package. The dot import is simply a syntaxic sugar… one that's not recommended to use by the way.

    If you really don't want to use the package renaming (which is the better solution IMHO), you can define your own methods to create the middlewares in your package.

    package middleware
    
    import "github.com/labstack/echo"
    import "github.com/labstack/echo/middleware"
    
    func Logger () echo.MiddlewareFunc {
        return middleware.Logger()
    }
    

    This would effectively remove the need for the echo/middleware package in you main code, at the cost of duplicate code that's not really needed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条