doz59484 2018-09-12 13:21
浏览 80
已采纳

在Go中创建包装器库时重新分配常量

So I want to create my own wrapper library from already existing lib A. Library A has some functions and also some constants, example:

package lib


const(
   HeaderA = "headerA"
   HeaderB = "headerB"
)

func doWork(string header) {
   // some work
}

Now the question is: Do I have to reassign these constants in my own library ?

package mylib
const (
    myHeaderA = lib.HeaderA
    myHeaderB = lib.HeaderB
)

What is the best practice in this case ? It seems ok to reassign them in order to make the user unaware of lib A, but what if this library has a ton of constants ?

Thank you

  • 写回答

1条回答 默认 最新

  • dongxie5698 2018-09-12 13:27
    关注

    There is no "package embedding", so constants from lib will not magically appear to be constants of mylib. If you want to provide those same constants in mylib, yes, you have to provide (copy) all those.

    but what if this library has a ton of constants ?

    Then on your mission to provide a wrapper for such a library that has a ton of constants, you also have to provide a ton of constants yourself (if the users of mylib do need all those constants).

    If you want to mimic the complete functionality of lib, that raises the question whether you really need this, and wouldn't it be simpler to just use lib instead of mylib.

    Note that you may create a utility that would generate a .go source file for mylib that would "copy" all exported constants of lib. Check out the go/parser package for a starting point.

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

报告相同问题?

悬赏问题

  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示