英文:
How to split a large package without breaking client code?
问题
假设我有一个名为foobar的库包。
随着时间的推移,它变得庞大而笨重。
幸运的是,它是可分离的,我成功地将其功能拆分为两个独立的包foo和bar--大多数客户只需要使用其中一个。
由于我的库已经被许多客户使用,为了兼容性,我仍然希望保留一个foobar包,作为当前在foo和bar中找到的功能的代理。
在Go语言中,如何实现这一点?
一个想法是在foobar中为foo和bar中的每个结构体/函数创建别名。所以如果foo定义了F()
,bar定义了B()
,那么在foobar中我会有:
var (
F = foo.F
B = bar.B
)
但我希望有一种更简单/更清晰的方法。
英文:
Let's say I have a library package called foobar.
Over time it became big and heavy.
Fortunately it's separable, I managed to split its functionality into two separate packages foo and bar -- most clients will only need to use one or the other.
Since my library is already in use by many clients, for compatibility I still want to maintain a foobar package as a proxy to the current functionality found in both foo and bar.
How does one achieve this in Go ?
One way that comes to mind is to create aliases in foobar for each struct/function in foo and bar. So if foo defines F()
and bar defines B()
, I would have in foobar:
var (
F = foo.F
B = bar.B
)
But I am hoping for an easier/cleaner way.
答案1
得分: 6
创建别名包是唯一的方法。
但是你的尝试可能不会成功:它只适用于函数、变量和常量,而不适用于类型。对于类型,你必须在 foobar 中复制该类型。
我不会这样做。只需在版本 1 中保留 foobar,并在 foo 和 bar 中重新开始(也许直接在版本 2 中开始)。
英文:
Creating an alias package is the only way.
But your attempt probably won't work: It works only for functions and variables and consts but not for types. For types you have to duplicate the type in foobar.
I wouldn't do this. Just have foobar around in version 1 and start anew with foo and bar (maybe directly in version 2).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论