dsjk3214 2019-04-26 18:02
浏览 71
已采纳

第三方库(封闭源)

Is there a way that you can build a binary file (shared or static library) in go language with bunch of source files and distribute it along with it's interfaces.

It's not to be distributed as an executable but a linkable static or shared library with interfaces at compile or run time. (Just like in C you distribute .a or .so file along with header files)

Unlike open source github based libraries out there it's a closed source project that i'm trying to port for Go.

  • 写回答

1条回答 默认 最新

  • dongxieyou3314 2019-04-26 18:18
    关注

    Yes it's possible . You can build your code as a static library and link it in runtime with this command :

    go build -buildmode=plugin
    

    You can use built plugin like this :

    A Symbol is a pointer to a variable or function.

    For example, a plugin defined as

    package main
    
    import "fmt"
    
    var V int
    
    func F() { fmt.Printf("Hello, number %d
    ", V) }
    

    may be loaded with the Open function and then the exported package symbols V and F can be accessed

    p, err := plugin.Open("plugin_name.so")
    if err != nil {
        panic(err)
    }
    v, err := p.Lookup("V")
    if err != nil {
        panic(err)
    }
    f, err := p.Lookup("F")
    if err != nil {
        panic(err)
    }
    *v.(*int) = 7
    f.(func())() // prints "Hello, number 7"
    
    
    type Symbol interface{}
    

    Check out built in support for Plugin in go for more info

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!