dsxay48646 2013-10-17 15:44
浏览 92
已采纳

从go二进制文件动态构建和链接

My problem is the following:

  1. I have a go binary on a machine
  2. From that binary I need to compile an external .go file
  3. Once compiled, I need to link the compiled go file into the current binary so I can use the just-compiled go code.

Do you think that's possible ?

I did a few researches and it does not seem to be possible, but I might have overlooked something.

Thanks :)

The first go binary would contain something like

func main() {
    // Here I need to compile an external go file (or package) which contains
    // The definition of runFoo()

    // Once the file/package is compiled and linked I need to call the compiled code
    runFoo()

    // Continue the execution process normally here
}
  • 写回答

5条回答 默认 最新

  • douhuang3740 2013-11-12 02:32
    关注

    Update: It is now possible do do this in mainline Go, see Go Execution Modes

    From the Go 1.5 release notes:

    For the amd64 architecture only, the compiler has a new option, -dynlink, that assists dynamic linking by supporting references to Go symbols defined in external shared libraries.

    Old Answer (useful discussion of other options):

    It is not currently possible to create dynamically linked libraries* in main line Go. There has been some talk about this, so you may see support in the future. However, there is a 3rd party go project called goandriod that needed the same functionality you need, so they maintain patches that should allow you to patch the official Go code base to support the dynamic linked support you are requesting.

    If you want to use a the standard Go run-time, I would recommend the one of the following. Invoke your Go program from your other program, and communicate using:

    1. Pipes to communicate
    2. A UNIX domain socket
    3. A mmaped region of shared memory.
      1. That is create a file on /dev/shm and have both programs mmap it.
      2. Go mmap library: https://github.com/edsrzf/mmap-go

    Each consecutive option will take more effort to setup, be more platform specific, but potentially be more powerful than the previous one.

    *Note: That is DLLs in the Windows world, and .so files in the UNIX/Linux world.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统