dopzc64662 2019-01-10 06:21
浏览 116
已采纳

有没有一种方法可以将Go可执行文件分离为多个文件?

I am shipping several executables compiled with Go. Each executable by itself doesn't contain a lot of code but each of them uses a common library that includes logging, configuration management, a communication layer, etc...

This leads to each executable being between 15-20mb sometimes for as little as 1000 own lines of code.

Is there a way in Go (currently available or planned for a future release) that will allow separating the application into several files (i.e. dlls in Windows, .so Linux/Mac)?

I know I can compile the library and then use it as external binary but then I will not get the benefits of the type system and the Go compiler optimization. Am I wrong here and there is a way to do it?

  • 写回答

2条回答 默认 最新

  • duanqun9618 2019-01-10 06:38
    关注

    The short answer: Kind of?

    It sounds like what you're trying to accomplish is a shared library.

    The Go compiler has been able to produce shared libraries since 1.5 with the -buildmode=c-shared build flag:

    go build -o helloworld.so -buildmode=c-shared
    

    And, as of Go 1.10, the functionality is additionally supported on Windows

    So, compiling to DLL is also a one-liner:

    go build -o helloworld.dll -buildmode=c-shared
    

    The problem that you're going to run into is actually using those libraries, especially in a cross operating system way:

    In *nix, you can use CGO to accomplish this:

    package example
    
    // #cgo LDFLAGS: -lfoo
    //
    // #include <foo.h>
    import "C"
    
    func main() {
        C.bar()
    }
    

    Windows gets its own Wiki (who's surprised?).

    I'll leave you with a few thoughts on this:

    1. There's nothing wrong with a 15-20mb binary (although, you should try upx to shave off some of that fat because why not?), you should be concerned when you're pushing 10s of 100s of gigs. Space is cheap these days.
    2. Writing something that can be compiled into a single binary, regardless of OS, is one of the best perks of Go.
    3. CGO_ENABLED=0 saves you a ton of space from your binaries. Keeping it enabled (which you need to use these features), isn't doing you any favors.
    4. You're right to assume that since the compiler can't optimize for included libraries, that ultimately you're not going to save much, if any space, in small use cases.

    My last point, then I'll stop preaching at you: Focus on writing code. Binary size should not be your concern unless you're trying to fit on embedded devices.

    Good luck friend.

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

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)