Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set the application icon in golang?

Tags:

icons

go

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.

like image 384
Gordon Truslove Avatar asked Sep 01 '14 09:09

Gordon Truslove


People also ask

How to SPecify an application icon c++?

Setting the Application Icon on Windows First, create an ICO format bitmap file that contains the icon image. This can be done with e.g. Microsoft Visual C++: Select "File|New...", then select the "File" tab in the dialog that appears, and choose "Icon".

How do I change the icon on my Godot project?

To change the taskbar icon, go to Project → Project Settings → Application → Config → Windows Native Icon. Click on the folder icon and select your ICO file. This setting only changes the icon for your exported game on Windows.


4 Answers

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.

like image 58
VonC Avatar answered Oct 24 '22 07:10

VonC


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

like image 45
Tody.Lu Avatar answered Oct 24 '22 06:10

Tody.Lu


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,
like image 4
jerblack Avatar answered Oct 24 '22 08:10

jerblack


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.

like image 3
RedGrittyBrick Avatar answered Oct 24 '22 08:10

RedGrittyBrick