Is there a way to reduce the binary size of a Cobra app

449 Views Asked by At

I'd like to use Cobra in a project where I have some strict binary size limitations. The binary size of a "hello world" Cobra app (as shown below) comes in at 4.1 MB. If I build it with go build -ldflags "-w -s" . then I can get it down to 2.8 MB. Does Cobra have any build tags (or other methods) to give up any functionality to further reduce the binary size? As an example, the Gin library has a -tags=nomsgpack flag to disable the MsgPack rendering feature. Does Cobra offer anything like that?

package main

import (
    "fmt"
    "os"

    "github.com/spf13/cobra"
)

func main() {
    if err := rootCmd.Execute(); err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
}

var rootCmd = &cobra.Command{
    Use:   "hugo",
    Short: "Hugo is a very fast static site generator",
    Long: `A Fast and Flexible Static Site Generator built with
                love by spf13 and friends in Go.
                Complete documentation is available at https://gohugo.io/documentation/`,
    Run: func(cmd *cobra.Command, args []string) {

    },
}
0

There are 0 best solutions below