dongpai9986 2017-03-16 05:25
浏览 84
已采纳

如何在编译时删除未使用的代码?

We've built a Go package that is used by many of us.

It's imported using the standard import ("package-name") method.

At compile time though, all of our utilities, including the very small ones, end up as very large binaries.

We've extracted all the strings in the utilities and discovered that the entire package is being compiled into each and every utility. Including functions that are not being used by those utilities.

EDIT 1:

Thank you to the people who are responding to this question.

Here's what we are seeing:

main.go

package main

import "play/subplay"

func main() {
    subplay.A()
}

play/subplay.go

package subplay

func A() {
    fmt.Printf("this is function A()")
}

func B() {
    fmt.Printf("secret string")
}

Function B() is never called. Yet, after building the binary, we find the string "secret string" into main.exe.

How can we remove unused code from Go programs at compile time?

  • 写回答

2条回答 默认 最新

  • duannuo4620 2017-03-16 07:41
    关注

    The compiler already does this. All code ends up in package files (.a), but in the executable binary the Go tool does not include everything from imported packages, only what is needed (or more precisely: it excludes things that it can prove unreachable).

    See possible duplicate Splitting client/server code.

    One thing to note here: if an imported package (from which you only want to include things you refer to) imports other packages, this has to be done recursively of course.

    For example if you import this package:

    package subplay
    
    func A() {}
    

    And call nothing from it:

    package main
    
    import _ "play/subplay"
    
    func main() {
    }
    

    The result binary (Go 1.8, linux, amd64) will be 960,134 bytes (roughly 1 MB).

    If you change subplay to import net/http:

    package subplay
    
    import _ "net/http"
    
    func A() {}
    

    But still don't call anything from net/http, the result will be: 5,370,935 bytes (roughly 5 MB)! (Note that net/http also imports 39 other packages!)

    Now if you go ahead and start using things from net/http:

    package subplay
    
    import "net/http"
    
    func A() {
        http.ListenAndServe("", nil)
    }
    

    But in the main package you still don't call subplay.A(), the size of the executable binary does not change: remains 5,370,935 bytes!

    And when you change the main package to call subplay.A():

    package main
    
    import "play/subplay"
    
    func main() {
        subplay.A()
    }
    

    The result binary grows: 5,877,919 bytes!

    If you change http.ListenAndServe() to http.ListenAndServeTLS():

    func A() {
        http.ListenAndServeTLS("", "", "", nil)
    }
    

    Result binary is: 6,041,535 bytes.

    As you can see, what gets compiled into the executable binary does depend on what you call / use from the imported packages.

    Also don't forget that Go is a statically linked language, the result executable binary has to include everything it needs. See related question: Reason for huge size of compiled executable of Go

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题