go – How do you set the application icon in golang?-ThrowExceptions

Exception or error:

I’ve just created my first go application on Windows.

How do I give it an icon?

There doesn’t seem to be any build flags to do this, and I know golang doesn’t support resources.

How to solve:

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

Answer:

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

Answer:

There is a github package GoVersionInfo by Joseph Spurrier:

Microsoft Windows File Properties/Version Info and Icon Resource Generator for the Go Language

Package creates a syso file which contains Microsoft Windows Version Information and an optional icon. When you run “go build”, Go will embed the version information and an optional icon and an optional manifest in the executable. Go will automatically use the syso file if it’s in the same directory as the main() function.

Answer:

None of the above answers worked for me. The only way I could get an ico embedded into a GO exe was with Resource Hacker.

http://www.angusj.com/resourcehacker/

  1. Install and open Resource Hacker
  2. Open compiled exe in Resource Hacker
  3. Action > Add an image or other binary resource
  4. Select ICO > Add Resource
  5. Save and Close

You can also run it directly from the command line if you need to add it to your build scripts.

ResourceHacker -open main.exe -save output.exe -action addskip -res main.ico -mask ICONGROUP,MAIN,

Leave a Reply

Your email address will not be published. Required fields are marked *