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 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码