如何处理包冲突而不重命名?

huangapple go评论70阅读模式
英文:

Go: How to Handle Package Collisions without Renaming?

问题

我正在尝试解决一个包冲突的问题,而不是重命名,因为我认为这不是特别优雅的解决方法。目前,我有自己的中间件来处理数据库连接,但我也使用了Echo的中间件。Echo也将其中间件的包名命名为"middleware"。

所以我尝试实现的解决方案是扩展Echo的包。但是我没有成功,并且没有找到任何关于如何做到这一点的信息。

你可以在这里查看Echo的中间件: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" //这是中间件的存储库
    "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()) //自定义中间件
    e.Use(middleware.Logger()) //Echo中间件
    e.Use(middleware.Recover()) //Echo中间件

    //编译失败,因为Logger和Recover没有被导出。

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

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

中间件结构

├── middleware
│    ├── db.go //自定义中间件
│    └── echo.go //Echo中间件

所以db.go和echo.go都被打包为middleware,但是我无法从导入的Echo包中"导出"函数。

echo.go

package middleware

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

//防止编译错误
//通常使用middleware.Logger来访问Echo中间件
//但是通过使用前面的点,你可以省略前缀
var _ = Logger()

尽管Logger()和Recover()函数的首字母大写,但GO编译失败,因为它们没有被导出,而db.go中的Db()函数被导出了。

英文:

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

得分: 1

你的导入解决方案不起作用,因为即使导入了echomiddleware包,它的导出标识符也不是你的middleware包的一部分。点导入只是一种语法糖...顺便说一句,这并不是推荐使用的方法。

如果你真的不想使用包重命名(这是我认为更好的解决方案),你可以在你的包中定义自己的方法来创建中间件。

package middleware

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

func Logger() echo.MiddlewareFunc {
    return middleware.Logger()
}

这样就可以在你的主要代码中省去对echo/middleware包的需求,代价是产生了一些不是真正需要的重复代码。

英文:

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.

huangapple
  • 本文由 发表于 2016年3月27日 18:24:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/36246285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定