Embedded Icon in Golang Windows Application

Install MinGW

MinGW is a compiler system uses GCC to produce Windows programs. Win32 ports of GCC, GDB, binutils to build native Win32 programs that rely on no 3rd party DLLs.

Install GCC

Install GCC (GNU C Compiler) from MinGW, select mingw32-gcc package from All PackagesMinGWMinGW Based System in left sidebar, and click Apply change in Installation menu.

Environment Variable Setting

Add MinGW key and C:\MinGW in operating system environment variable and set C:\MinGW\bin append to Path value. After do that, checking version info of gcc in command prompt window by gcc -v command.

C:\Documents and Settings\Administrator\go_res>gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.9.3/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-4.9.3/configure --build=x86_64-pc-linux-gnu --host=mingw32 --prefix=/mingw --disable-win32-registry --target=mingw32 --with-arch=i586 --enable-languages=c,c++,objc,obj-c++,fortran,ada --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --enable-libstdcxx-debug --with-tune=generic --enable-nls
Thread model: win32
gcc version 4.9.3 (GCC)

Create main.exe.manifest file and put following content into it:

<?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>

Create main.rc file and put following content into it:

IDI_ICON1 ICON "icon.ico"
1 24 "main.exe.manifest"

Also add version info:

1 VERSIONINFO
FILEVERSION     1,0,0,0
PRODUCTVERSION  1,0,0,0
FILEFLAGS       0x0L
FILEFLAGSMASK   0x3fL
FILESUBTYPE     0
BEGIN
    BLOCK "StringFileInfo"
    BEGIN
        BLOCK "040904B0"
        BEGIN
            VALUE "FileDescription", "File description"
            VALUE "FileVersion", "1.0"
            VALUE "LegalCopyright", "Copyright"
            VALUE "InternalName", "Internel name"
            VALUE "OriginalFilename", "File name"
            VALUE "ProductName", "Product name"
            VALUE "ProductVersion", "1.0"
        END
    END
    BLOCK "VarFileInfo"
    BEGIN
            VALUE "Translation", 0x0409, 0x04B0
    END
END

1 ICON "icon.ico"

1 RT_MANIFEST "main.exe.manifest"

icon.ico is the name of the application icon, make sure put the icon file in the same directory with main.exe.manifest, main.rc and main.go files.

Build and Embedded Icon

Extract windres.exe from MinGW installer, we can download from SourceForge. Use windres.exe generate a .syso file, the golang compiler will automatically search and link this file in current compile directory.

C:\Documents and Settings\Administrator\go_res>windres -o main-res.syso main.rc && go build -i
C:\Documents and Settings\Administrator\go_res>dir
 Volume in driver C has no label.
 Volume Serial Number is E43D-2258

 Directory of C:\Documents and Settings\Administrator\go_res

2016-06-15  15:27    <DIR>          .
2016-06-15  15:27    <DIR>          ..
2016-06-15  15:07           150,876 icon.ico
2016-06-15  15:22           152,624 main-res.syso
2016-06-15  15:22         2,144,256 go_res.exe
2016-06-15  15:10               603 main.exe.manifest
2016-06-15  15:00                77 main.go
2016-06-15  15:13                51 main.rc
2011-11-30  19:20         1,811,470 windres.exe

Reference

MSDN: Resource-Definition Statements - VERSIONINFO resource

Embedded Icon in Golang Windows Application
8 votes, 4.88 avg. rating (97% score)