3

Reduce Go Binary Size

 1 year ago
source link: https://gophercoding.com/reduce-go-binary-size/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Reduce Go Binary Size · Gopher Coding

Written by Edd Turtle on gophercoding.com
on 6th of April 2023
(Updated: 9th of April 2023)

Reduce Go Binary Size

Reduce Go Binary Size

Always a hot-topic in Go circles is binary size of applications. In this post we will show you how you can reduce the binary size produced from go build for leaner, slimer builds.

In short, adding the ldflags shown below will reduce the size of the binary produced. It does this by removing some perhaps non-critial parts (shouldn’t affect execution, only debugging potentially).

-s Will turn off the symbol table, so you won’t be able to use commands like go tool nm.

-w Turns off the ‘DWARF’ debugging information, so you won’t be able to set breakpoints or get specific information in stacktraces. Likewise, certain profile use cases won’t work, like pprof.

$
go build -ldflags="-s -w"

For example, we’ll take our code from our post about sha3 encoding. If we run the build using Go v1.19 then we get a binary size of 1.8M, but using the ldflags above it will produce a binary size of 1.2M (so a 33% decrease). While this reduction is ground-breaking, it has a bigger impact on larger applications.

How can I check if an application has been stripped?

We can use the same example as above, but use the file command on it. This will tell us if it was stripped of debug information or not.

$
$ go build sha3.go && file sha3
sha3: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, 
Go BuildID={id}, with debug_info, not stripped

The above is for normal build, notice with debug_info. The below is for a stripped build.

$
$ go build -ldflags="-s -w" sha3.go && file sha3
sha3: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, 
Go BuildID={id}, stripped

fi-play-circle.svg Example In Action

golang reduce binary size
Author Edd Turtle

Edd is a PHP and Go developer who enjoys blogging about his experiences, mostly about creating and coding new things he's working on and is a big beliver in open-source and Linux.

Tweet me! ☕   Buy me a Coffee (Paypal)

Related Posts

  • Using Iota for a List of Constants (Like Enums) – Apr 4, 2023
    Enums have been used in many programming languages in varying forms for many years. But if you’ve found this post, then you are most likely looking for how to do this in Go (golang). In Go, there’s a handy feature called iota which is a running list of ever-increasing constants. They are a consise way of presenting a list of things in a “don’t repeat yourself” way. For example if you need to define days of the week using contants, you could:
  • How to Die Dump in Go - dd() – Nov 6, 2022
    Having come from the PHP community, we often have a handy debug function at our disposal dd() (part of Laravel) and var_dump() (native). This may not be perfect (compared with a full on debugging suite) but it is a quick and easy debugging method. This post gives an idea on how you can do the same, but in Go. We’re helped out by being able to use both variable parameters (shown with .
  • Remove All Non-Alphanumeric Characters – Sep 23, 2022
    We often need to remove symbols and special characters from the strings we’re using (especially with currency!). This post shows how you can keep the letters and numbers, but remove any punctuation, symbols, grammar, etc. For example, if a user types in “$1,000” you can turn it into “1000”. We use the regexp package to do this, first building a regex with .Compile() then running the string through that regex with .
↑   Back to Top   ↑Privacy Policy
Site illustrations (of Goldie the Bird) drawn by Carina Roberts.
Tutorials & Site Content © 2023 Gopher Coding
Code Licenced under MIT Licence Gopher Coding Logo

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK