Compress go build packaged executables--3.4MB-897K

Source: Internet
Author: User
Tags etcd
This is a creation in Article, where the information may have evolved or changed. # # Preface recently in writing a CLI program, after the habitual go build found a simple few lines of code packaged out 3.0+MB, for this was made Python classmate laughed for a long time, and then studied the ' Go build ' generated executable file compression # # The code sample code is as follows: "Package Mainimport (" OS "" Gopkg.in/urfave/cli.v2 ") const (app_name =" etcd-cli "app_version =" 0.1 "App_usage = "ETCD-CLI is a simple and practical ETCD command line client that helps you to completely free time and time again for input Etcdctl") Func Main () {app: = new (CLI. APP) app. Name = App_nameapp.version = App_versionapp.usage = App_usageapp.action = Actionapp. Run (OS. Args)}func action (CTX *CLI. Context) error {return nil} ' # # # Normal packaging method and results ' ' $ go build$ ls-lh-rwxr-xr-x 1 Gangan staff 3.4M 00:38 etcd-cli-rw-r-- r--1 Gangan Staff 456B 00:34 main.go "You can see that a mere more than 10 lines of code has not been implemented, ' go build ' after a 3.4MB executable, imagine the product development after the ' Go build ' never will be small, then there is no solution? There, see below. # # Zip Executable-First add compile parameter '-ldflags ' ' $ go build-ldflags '-w-s ' $ ls-lh-rwxr-xr-x 1 Gangan staff 2.5M 00:45 etcd-c li-rw-r--r--1 Gangan Staff 456B 00:34 main.go "can be found small near 1mb-using UPX compression, Linux, Mac and win all have, here to Mac for example" ' $ brew Install UPX $ upx Etcd-cli # here to omit compression when printing ...$ ls-lh-rwxr-xr-x 1 Gangan Staff 897K 00:49 etcd-cli-rw-r--r--1 Gangan staff 456B 00:34 main.go "OK, can Execution files from 3.4MB compression to 897K, the effect is very obvious, there is a need to quickly try it. # # written in the last Golang development program will be relatively large, this is because Golang is statically compiled, after the compilation of the basic will not be more dependent on other class libraries, so it will be relatively large. For example: C + + program can call DLL, so when packaging can not put the DLL into the package naturally small. I've seen someone use the ' Go-C---DLL--c-and go ' way to indirectly implement the pseudo-dynamic link of Golang, interested students can study. # # Add Linux, Win system under UPX https://github.com/upx/upx/releases209 click  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.