How do you set the application icon in golang?

The topic is long time, in fact mingw is only requirement, we don't need 3rd party dependency. In addition, resource file *.rc is mandatory for win32 executable application. At last, you can find the demo in rc-demo

1) Install mingw using Chocolatey: choco install mingw

2) Create main.exe.manifest file

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
    version="1.0.0.0"
    processorArchitecture="x86"
    name="controls"
    type="win32"
/>
<dependency>
    <dependentAssembly>
        <assemblyIdentity
            type="win32"
            name="Microsoft.Windows.Common-Controls"
            version="6.0.0.0"
            processorArchitecture="*"
            publicKeyToken="6595b64144ccf1df"
            language="*"
        />
    </dependentAssembly>
</dependency>
</assembly>

3) Create main.rc file

100 ICON    "main.ico"
100 24      "main.exe.manifest"
101 RCDATA  "content.zip"

4) Build

In git-bash windows perform the following command: windres -o main-res.syso main.rc && go build -i


You can use a tool like akavel/rsrc in order to generate a .syso file with specified resources embedded in .rsrc section, aimed for consumption by Go linker when building Win32 excecutables.

See as an example the lxn/walk application, which embeds other metadata in its executable.

rsrc [-manifest FILE.exe.manifest] [-ico FILE.ico[,FILE2.ico...]] -o FILE.syso

-ico="": comma-separated list of paths to .ico files to embed


This differs from embedding binary data into a go program.
For that, use jteeuwen/go-bindata.

To access asset data, we use the Asset(string) []byte function which is included in the generated output.

data := Asset("pub/style/foo.css")
if len(data) == 0 {
    // Asset was not found.
}

// use asset data

Update 2021: as noted in John N's answer, you can also use:

github.com/tc-hib/go-winres

A simple command line tool for embedding usual resources in Windows executables built with Go:

  • A manifest
  • An application icon
  • Version information (the Details tab in file properties)
  • Other icons and cursors

It can be used as a library too.

Tags:

Icons

Go