goversioninfo

package module
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2026 License: MIT Imports: 15 Imported by: 84

README

GoVersionInfo

Go Report Card Unit Tests Coverage Status GoDoc

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.

Example of the file properties you can set using this package:

Image of File Properties

Usage

To install, run the following command:

go install github.com/josephspurrier/goversioninfo/cmd/goversioninfo@latest

Copy testdata/resource/versioninfo.json into your working directory and then modify the file with your own settings.

Add a similar text to the top of your Go source code (-icon and -manifest are optional, but can also be specified in the versioninfo.json file):

//go:generate goversioninfo -icon=testdata/resource/icon.ico -manifest=testdata/resource/goversioninfo.exe.manifest

Run the Go commands in this order so goversioninfo will create a file called resource.syso in the same directory as the Go source code.

go generate
go build

Architecture Detection

The -64 and -arm flags default based on the GOARCH environment variable (falling back to the host architecture if GOARCH is not set). This means go generate will automatically produce a resource file matching the target architecture without needing to pass -64 or -arm explicitly. You can still override the defaults by passing the flags on the command line.

Version Synchronization

The FixedFileInfo and StringFileInfo sections of the JSON config both contain FileVersion and ProductVersion fields. FixedFileInfo stores them as structured numeric components (Major, Minor, Patch, Build), while StringFileInfo stores them as free-form strings.

When Build() is called, missing version fields are automatically filled in:

  • If FixedFileInfo has a version set but the corresponding StringFileInfo string is empty, the string is generated (e.g., "2.0.0.0").
  • If StringFileInfo has a parseable version string but the corresponding FixedFileInfo fields are all zero, the struct is populated from the string.
  • If both are already set, neither is modified — but a warning is logged if the numeric components do not match.
  • If a StringFileInfo version string cannot be parsed as a version number (e.g., x.y.z or x.y.z.w), a warning is logged.

This means you only need to specify version information in one place. For example, providing just FixedFileInfo is sufficient:

{
  "FixedFileInfo": {
    "FileVersion": {
      "Major": 2,
      "Minor": 0,
      "Patch": 0,
      "Build": 0
    },
    "ProductVersion": {
      "Major": 2,
      "Minor": 0,
      "Patch": 0,
      "Build": 0
    }
  }
}

Application Icon (Window Title Bar)

By default, Windows uses the system default icon for the window title bar. To set a custom window icon, goversioninfo embeds an icon resource with the IDI_APPLICATION resource ID (32512). This is the icon that Win32 applications load via LoadIcon(hInstance, IDI_APPLICATION).

When IconPath is set and ApplicationIconPath is not, the application icon defaults to the same file as IconPath. To use a different icon for the window title bar, set ApplicationIconPath explicitly:

{
    "IconPath": "icons/main.ico",
    "ApplicationIconPath": "icons/small.ico"
}

You can also set it via the command line:

goversioninfo -icon=icons/main.ico -application-icon=icons/small.ico

If neither IconPath nor ApplicationIconPath is set, no application icon is embedded.

Command-Line Flags

Complete list of the flags for goversioninfo:

  -charset=0: charset ID
  -comment="": StringFileInfo.Comments
  -company="": StringFileInfo.CompanyName
  -copyright="": StringFileInfo.LegalCopyright
  -description="": StringFileInfo.FileDescription
  -example=false: dump out an example versioninfo.json to stdout
  -file-version="": StringFileInfo.FileVersion
  -icon="": icon file name(s), separated by commas
  -application-icon="": icon file for IDI_APPLICATION (window title bar); defaults to -icon if unset
  -internal-name="": StringFileInfo.InternalName
  -manifest="": manifest file name
  -skip-versioninfo=false: skip version info reading on true, allows setting just icon
  -o="resource.syso": output file name
  -gofile="": Go output file name (optional) - generates a Go file to access version information internally
  -gofilepackage="main": Go output package name (optional, requires parameter: 'gofile')
  -platform-specific=false: output i386 and amd64 named resource.syso, ignores -o
  -original-name="": StringFileInfo.OriginalFilename
  -private-build="": StringFileInfo.PrivateBuild
  -product-name="": StringFileInfo.ProductName
  -product-version="": StringFileInfo.ProductVersion
  -special-build="": StringFileInfo.SpecialBuild
  -trademark="": StringFileInfo.LegalTrademarks
  -translation=0: translation ID
  -64:false: generate 64-bit binaries on true
  -arm:false: generate ARM binaries on true
  -ver-major=-1: FileVersion.Major
  -ver-minor=-1: FileVersion.Minor
  -ver-patch=-1: FileVersion.Patch
  -ver-build=-1: FileVersion.Build
  -product-ver-major=-1: ProductVersion.Major
  -product-ver-minor=-1: ProductVersion.Minor
  -product-ver-patch=-1: ProductVersion.Patch
  -product-ver-build=-1: ProductVersion.Build

You can look over the Microsoft Resource Information: VERSIONINFO resource

You can look through the Microsoft Version Information structures: Version Information Structures

PowerShell Differences

In PowerShell, the version components are named differently than the fields in the versioninfo.json file:

PowerShell:          versioninfo.json:
-----------          -----------------
FileMajorPart      = FileVersion.Major
FileMinorPart      = FileVersion.Minor
FileBuildPart      = FileVersion.Patch
FilePrivatePart    = FileVersion.Build
ProductMajorPart   = ProductVersion.Major
ProductMinorPart   = ProductVersion.Minor
ProductBuildPart   = ProductVersion.Patch
ProductPrivatePart = ProductVersion.Build

If you find any other differences, let me know.

Unicode Characters in versioninfo.json

The versioninfo.json file must be saved as UTF-8. If your editor saves it in a different encoding (such as Windows-1252, which is the default for some Windows text editors), non-ASCII characters like the copyright symbol © will appear as ? or in the compiled executable's file properties.

This happens because Go reads the JSON file as UTF-8. A © saved as Windows-1252 is a single byte (0xA9), which is invalid UTF-8. Go replaces invalid bytes with the Unicode replacement character (U+FFFD), which Windows then displays as ?.

To fix this, either:

  • Save versioninfo.json as UTF-8 in your text editor, or
  • Use the JSON escape sequence © instead of the literal © character

Alternatives to this Tool

You can also use windres to create the syso file. The windres executable is available in either MinGW or tdm-gcc.

Below is a sample batch file you can use to create a .syso file from a .rc file. There are sample .rc files in the testdata/rc folder.

@ECHO OFF

SET PATH=C:\TDM-GCC-64\bin;%PATH%
REM SET PATH=C:\mingw64\bin;%PATH%

windres -i testdata/rc/versioninfo.rc -O coff -o versioninfo.syso

PAUSE

The information on how to create a .rc file is available here. You can use the testdata/rc/versioninfo.rc file to create a .syso file that contains version info, icon, and manifest.

Issues

The majority of the code for the creation of the syso file is from this package: https://github.com/akavel/rsrc

There is an issue with adding the icon resource that prevents your application from being compressed or modified with a resource editor. Please use with caution.

Major Contributions

Thanks to Tamás Gulácsi for his superb code additions, refactoring, and optimization to make this a solid package.

Thanks to Mateusz Czaplinski for his embedded binary resource package with icon and manifest functionality.

Documentation

Overview

Package goversioninfo creates a syso file which contains Microsoft Version Information and an optional icon.

Example
package main

import (
	"fmt"
)

func main() {
	fmt.Println("Hello world")
}

Index

Examples

Constants

View Source
const (
	Cs7ASCII       = CharsetID(0)    // Cs7ASCII:	0	0000	7-bit ASCII
	CsJIS          = CharsetID(932)  // CsJIS:	932	03A4	Japan (Shift ? JIS X-0208)
	CsKSC          = CharsetID(949)  // CsKSC:	949	03B5	Korea (Shift ? KSC 5601)
	CsBig5         = CharsetID(950)  // CsBig5:	950	03B6	Taiwan (Big5)
	CsUnicode      = CharsetID(1200) // CsUnicode:	1200	04B0	Unicode
	CsLatin2       = CharsetID(1250) // CsLatin2:	1250	04E2	Latin-2 (Eastern European)
	CsCyrillic     = CharsetID(1251) // CsCyrillic:	1251	04E3	Cyrillic
	CsMultilingual = CharsetID(1252) // CsMultilingual:	1252	04E4	Multilingual
	CsGreek        = CharsetID(1253) // CsGreek:	1253	04E5	Greek
	CsTurkish      = CharsetID(1254) // CsTurkish:	1254	04E6	Turkish
	CsHebrew       = CharsetID(1255) // CsHebrew:	1255	04E7	Hebrew
	CsArabic       = CharsetID(1256) // CsArabic:	1256	04E8	Arabic
)

CharsetID constants

View Source
const (
	LngArabic                = LangID(0x0401) // LngArabic: 0x0401 Arabic
	LngBulgarian             = LangID(0x0402) // LngBulgarian: 0x0402 Bulgarian
	LngCatalan               = LangID(0x0403) // LngCatalan: 0x0403 Catalan
	LngTraditionalChinese    = LangID(0x0404) // LngTraditionalChinese: 0x0404 Traditional Chinese
	LngCzech                 = LangID(0x0405) // LngCzech: 0x0405 Czech
	LngDanish                = LangID(0x0406) // LngDanish: 0x0406 Danish
	LngGerman                = LangID(0x0407) // LngGerman: 0x0407 German
	LngGreek                 = LangID(0x0408) // LngGreek: 0x0408 Greek
	LngUSEnglish             = LangID(0x0409) // LngUSEnglish: 0x0409 U.S. English
	LngCastilianSpanish      = LangID(0x040A) // LngCastilianSpanish: 0x040A Castilian Spanish
	LngFinnish               = LangID(0x040B) // LngFinnish: 0x040B Finnish
	LngFrench                = LangID(0x040C) // LngFrench: 0x040C French
	LngHebrew                = LangID(0x040D) // LngHebrew: 0x040D Hebrew
	LngHungarian             = LangID(0x040E) // LngHungarian: 0x040E Hungarian
	LngIcelandic             = LangID(0x040F) // LngIcelandic: 0x040F Icelandic
	LngItalian               = LangID(0x0410) // LngItalian: 0x0410 Italian
	LngJapanese              = LangID(0x0411) // LngJapanese: 0x0411 Japanese
	LngKorean                = LangID(0x0412) // LngKorean: 0x0412 Korean
	LngDutch                 = LangID(0x0413) // LngDutch: 0x0413 Dutch
	LngNorwegianBokmal       = LangID(0x0414) // LngNorwegianBokmal: 0x0414 Norwegian ? Bokmal
	LngPolish                = LangID(0x0415) // LngPolish: 0x0415 Polish
	LngPortugueseBrazil      = LangID(0x0416) // LngPortugueseBrazil: 0x0416 Portuguese (Brazil)
	LngRhaetoRomanic         = LangID(0x0417) // LngRhaetoRomanic: 0x0417 Rhaeto-Romanic
	LngRomanian              = LangID(0x0418) // LngRomanian: 0x0418 Romanian
	LngRussian               = LangID(0x0419) // LngRussian: 0x0419 Russian
	LngCroatoSerbianLatin    = LangID(0x041A) // LngCroatoSerbianLatin: 0x041A Croato-Serbian (Latin)
	LngSlovak                = LangID(0x041B) // LngSlovak: 0x041B Slovak
	LngAlbanian              = LangID(0x041C) // LngAlbanian: 0x041C Albanian
	LngSwedish               = LangID(0x041D) // LngSwedish: 0x041D Swedish
	LngThai                  = LangID(0x041E) // LngThai: 0x041E Thai
	LngTurkish               = LangID(0x041F) // LngTurkish: 0x041F Turkish
	LngUrdu                  = LangID(0x0420) // LngUrdu: 0x0420 Urdu
	LngBahasa                = LangID(0x0421) // LngBahasa: 0x0421 Bahasa
	LngSimplifiedChinese     = LangID(0x0804) // LngSimplifiedChinese: 0x0804 Simplified Chinese
	LngSwissGerman           = LangID(0x0807) // LngSwiss German: 0x0807 Swiss German
	LngUKEnglish             = LangID(0x0809) // LngUKEnglish: 0x0809 U.K. English
	LngSpanishMexico         = LangID(0x080A) // LngSpanishMexico: 0x080A Spanish (Mexico)
	LngBelgianFrench         = LangID(0x080C) // LngBelgian French: 0x080C Belgian French
	LngSwissItalian          = LangID(0x0810) // LngSwiss Italian: 0x0810 Swiss Italian
	LngBelgianDutch          = LangID(0x0813) // LngBelgian Dutch: 0x0813 Belgian Dutch
	LngNorwegianNynorsk      = LangID(0x0814) // LngNorwegianNynorsk: 0x0814 Norwegian ? Nynorsk
	LngPortuguesePortugal    = LangID(0x0816) // LngPortuguese (Portugal): 0x0816 Portuguese (Portugal)
	LngSerboCroatianCyrillic = LangID(0x081A) // LngSerboCroatianCyrillic: 0x081A Serbo-Croatian (Cyrillic)
	LngCanadianFrench        = LangID(0x0C0C) // LngCanadian French: 0x0C0C Canadian French
	LngSwissFrench           = LangID(0x100C) // LngSwiss French: 0x100C Swiss French
)

LangID constants

Variables

This section is empty.

Functions

func RunCLI added in v1.7.0

func RunCLI(cfg CLIConfig) error

RunCLI generates version info resource files based on the provided CLIConfig.

Types

type CLIConfig added in v1.7.0

type CLIConfig struct {
	ConfigFile          string
	OutputFile          string
	GoFile              string
	GoFilePackage       string
	PlatformSpecific    bool
	IconPath            string
	ApplicationIconPath string
	ManifestPath        string
	SkipVersionInfo     bool
	PropagateVerStrings bool

	Comment        string
	CompanyName    string
	Description    string
	FileVersion    string
	InternalName   string
	Copyright      string
	Trademark      string
	OriginalName   string
	PrivateBuild   string
	ProductName    string
	ProductVersion string
	SpecialBuild   string

	TranslationID int
	CharsetID     int

	Is64Bit bool
	IsARM   bool

	// Version override fields use -1 to mean "don't override."
	// Use NewCLIConfig to get these defaults.
	VerMajor int
	VerMinor int
	VerPatch int
	VerBuild int

	ProductVerMajor int
	ProductVerMinor int
	ProductVerPatch int
	ProductVerBuild int
}

CLIConfig holds all settings for generating a version info resource. Use NewCLIConfig to get a CLIConfig with sensible defaults.

func NewCLIConfig added in v1.7.0

func NewCLIConfig() CLIConfig

NewCLIConfig returns a CLIConfig with sensible defaults matching the CLI behavior.

type CharsetID

type CharsetID uint16

CharsetID must use be a character-set identifier from: https://msdn.microsoft.com/en-us/library/windows/desktop/aa381058(v=vs.85).aspx#charsetID

func (*CharsetID) UnmarshalJSON

func (cs *CharsetID) UnmarshalJSON(p []byte) error

UnmarshalJSON converts the string to a CharsetID

type FileVersion

type FileVersion struct {
	Major int
	Minor int
	Patch int
	Build int
}

FileVersion with 3 parts.

func NewFileVersion added in v1.5.0

func NewFileVersion(version string) (FileVersion, error)

NewFileVersion parses semver version string into a FileVersion object

func (FileVersion) GetVersionString

func (f FileVersion) GetVersionString() string

GetVersionString returns a string representation of the version

func (FileVersion) IsZero added in v1.7.0

func (f FileVersion) IsZero() bool

IsZero returns true if all version components are zero.

type FixedFileInfo

type FixedFileInfo struct {
	FileVersion    `json:"FileVersion"`
	ProductVersion FileVersion
	FileFlagsMask  string
	FileFlags      string
	FileOS         string
	FileType       string
	FileSubType    string
}

FixedFileInfo contains file characteristics - leave most of them at the defaults.

type LangID

type LangID uint16

LangID must use be a character-set identifier from: https://msdn.microsoft.com/en-us/library/windows/desktop/aa381058(v=vs.85).aspx#langID

func (*LangID) UnmarshalJSON

func (lng *LangID) UnmarshalJSON(p []byte) error

UnmarshalJSON converts the string to a LangID

type SizedReader

type SizedReader struct {
	*bytes.Buffer
}

SizedReader is a *bytes.Buffer.

func (SizedReader) Size

func (s SizedReader) Size() int64

Size returns the length of the buffer.

type StringFileInfo

type StringFileInfo struct {
	Comments         string
	CompanyName      string
	FileDescription  string
	FileVersion      string
	InternalName     string
	LegalCopyright   string
	LegalTrademarks  string
	OriginalFilename string
	PrivateBuild     string
	ProductName      string
	ProductVersion   string
	SpecialBuild     string
}

StringFileInfo is what you want to change.

type Translation

type Translation struct {
	LangID    `json:"LangID"`
	CharsetID `json:"CharsetID"`
}

Translation with langid and charsetid.

type VSFixedFileInfo

type VSFixedFileInfo struct {
	DwSignature        uint32
	DwStrucVersion     uint32
	DwFileVersionMS    uint32
	DwFileVersionLS    uint32
	DwProductVersionMS uint32
	DwProductVersionLS uint32
	DwFileFlagsMask    uint32
	DwFileFlags        uint32
	DwFileOS           uint32
	DwFileType         uint32
	DwFileSubtype      uint32
	DwFileDateMS       uint32
	DwFileDateLS       uint32
}

VSFixedFileInfo - most of these should be left at the defaults.

type VSString

type VSString struct {
	WLength      uint16
	WValueLength uint16
	WType        uint16
	SzKey        []byte
	Padding      []byte
	Value        []byte
}

VSString holds the keys and values.

type VSStringFileInfo

type VSStringFileInfo struct {
	WLength      uint16
	WValueLength uint16
	WType        uint16
	SzKey        []byte
	Padding      []byte
	Children     VSStringTable
}

VSStringFileInfo holds multiple collections of keys and values, only allows for 1 collection in this package.

type VSStringTable

type VSStringTable struct {
	WLength      uint16
	WValueLength uint16
	WType        uint16
	SzKey        []byte
	Padding      []byte
	Children     []VSString
}

VSStringTable holds a collection of string keys and values.

type VSVar

type VSVar struct {
	WLength      uint16
	WValueLength uint16
	WType        uint16
	SzKey        []byte
	Padding      []byte
	Value        uint32
}

VSVar holds the translation key.

type VSVarFileInfo

type VSVarFileInfo struct {
	WLength      uint16
	WValueLength uint16
	WType        uint16
	SzKey        []byte
	Padding      []byte
	Value        VSVar
}

VSVarFileInfo holds the translation collection of 1.

type VSVersionInfo

type VSVersionInfo struct {
	WLength      uint16
	WValueLength uint16
	WType        uint16
	SzKey        []byte
	Padding1     []byte
	Value        VSFixedFileInfo
	Padding2     []byte
	Children     VSStringFileInfo
	Children2    VSVarFileInfo
}

VSVersionInfo is the top level version container.

type VarFileInfo

type VarFileInfo struct {
	Translation `json:"Translation"`
}

VarFileInfo is the translation container.

type VersionInfo

type VersionInfo struct {
	FixedFileInfo       `json:"FixedFileInfo"`
	StringFileInfo      `json:"StringFileInfo"`
	VarFileInfo         `json:"VarFileInfo"`
	Timestamp           bool
	Buffer              bytes.Buffer
	Structure           VSVersionInfo
	IconPath            string `json:"IconPath"`
	ManifestPath        string `json:"ManifestPath"`
	ApplicationIconPath string `json:"ApplicationIconPath"`
}

VersionInfo data container

func (*VersionInfo) Build

func (v *VersionInfo) Build()

Build fills the structs with data from the config file

func (*VersionInfo) ParseJSON

func (vi *VersionInfo) ParseJSON(jsonBytes []byte) error

ParseJSON parses the given bytes as a VersionInfo JSON.

func (*VersionInfo) Walk

func (vi *VersionInfo) Walk()

Walk writes the data buffer with hexadecimal data from the structs

func (*VersionInfo) WriteGo added in v1.2.0

func (vi *VersionInfo) WriteGo(filename, packageName string) error

WriteGo creates a Go file that contains the version info so you can access it in the application

func (*VersionInfo) WriteHex

func (vi *VersionInfo) WriteHex(filename string) error

WriteHex creates a hex file for debugging version info

func (*VersionInfo) WriteSyso

func (vi *VersionInfo) WriteSyso(filename string, arch string) error

WriteSyso creates a resource file from the version info and optionally an icon. arch must be an architecture string accepted by coff.Arch, like "386" or "amd64"

Directories

Path Synopsis
cmd
goversioninfo command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL