跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • Golang 编译的时候,是不是把第三库所有的函数都编译进去了
未分類
30 12 月 2020

Golang 编译的时候,是不是把第三库所有的函数都编译进去了

Golang 编译的时候,是不是把第三库所有的函数都编译进去了

資深大佬 : wellhome 12

引用了一个第三方包, 只用到其中一部分函数,那些没有用到的函数是不是也被编译到静态二进制文件中了?
大佬有話說 (8)

  • 資深大佬 : lemy

    是的。而且不止 golang,很多方法都是。

  • 資深大佬 : cmostuor

    只包含要用到的(不管系统库还是第三库) 没用到的不编译 不信反编译看看

  • 資深大佬 : Liyiw

    上面两位究竟谁对谁错

  • 資深大佬 : Vegetable

    不是。在 1.7 版本引入了 method pruning,没有引用和反射的方法将不会被打包。
    参见

    https://blog.golang.org/go1.7-binary-size

    The second change is method pruning. Until 1.6, all methods on all used types were kept, even if some of the methods were never called. This is because they might be called through an interface, or called dynamically using the reflect package. Now the compiler discards any unexported methods that do not match an interface. Similarly the linker can discard other exported methods, those that are only accessible through reflection, if the corresponding reflection features are not used anywhere in the program. That change shrinks binaries by 5–20%.

  • 資深大佬 : icy37785

    @Liyiw 二的是对的,一的是错的。

  • 資深大佬 : Vegetable

    关于函数本身而不是方法。并没有明确的规范说明未使用的函数究竟会不会打包到二进制文件,不过目前验证来看也是会丢弃的,丢弃这个远比方法简单,因为 Go 中函数是不能反射的,直接丢掉就行了。

  • 資深大佬 : SingeeKing

    我感觉好像只有 Java 会这样,其他的「编译型」语言连 js 都有 Tree Shaking 了

  • 主 資深大佬 : wellhome

    那这点比 python 强阿。python 用个第三方包中的某个程序,要把整个包都装一下。

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具