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.
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">

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

IDI_ICON1 ICON "icon.ico"
1 24 "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
Embedded Icon in Golang Windows Application
8 votes, 4.88 avg. rating (97% score)