Go: Difference between revisions
Appearance
NickPGSmith (talk | contribs) |
NickPGSmith (talk | contribs) No edit summary |
||
| (8 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
== Install and Update == | |||
winget install GoLang.Go | |||
go winget upgrade GoLang.Go | |||
go mod edit -go=1.26 # Replace with the version you just installed | |||
go mod tidy | |||
== Building and Packages == | == Building and Packages == | ||
| Line 4: | Line 12: | ||
go build test.go | go build test.go | ||
If there are multiple files in the main package, must make go access all of them: | |||
go run . | |||
go build . | |||
To remove debug symbols: | |||
go build -ldflags="-s -w" . | |||
In multi-file projects, main.go is the convention. The binary name is taken from the project directory. | |||
gofmt -w . | gofmt -w . | ||
gofmt -d test.go | gofmt -d test.go | ||
Initialise project, create go.mod file: | Initialise project, create go.mod file: | ||
| Line 19: | Line 34: | ||
Note the go.sum file that contains anti-tampering checksums. | Note the go.sum file that contains anti-tampering checksums. | ||
To cross compile: | |||
GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o disco_scan_updater | |||
GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o disco_scan_updater.exe | |||
Show available platforms: | |||
go tool dist list | |||
=== Windows Executables with winres === | |||
To add icons and version details: | |||
go install github.com/tc-hib/go-winres@latest | |||
~/go/bin/go-winres init | |||
Add resources: | |||
* Edit winres/winres.json (icon reference: RT_GROUP_ICON) | |||
* Add winres/icon.png (s single high resolution PNG will be rescaled automatically | |||
Make syso resources, and build into binary: | |||
~/go/bin/go-winres make | |||
go build | |||
=== Useful Packages === | |||
* [https://github.com/alexflint/go-arg alexflint/go-arg] | * [https://github.com/alexflint/go-arg alexflint/go-arg] | ||
* | * [https://github.com/pelletier/go-toml https://github.com/pelletier/go-toml] | ||
* [https://github.com/shirou/gopsutil https://github.com/shirou/gopsutil] | |||
* [https://github.com/dlclark/regexp2 https://github.com/dlclark/regexp2] | |||
Latest revision as of 11:08, 13 March 2026
Install and Update
winget install GoLang.Go
go winget upgrade GoLang.Go go mod edit -go=1.26 # Replace with the version you just installed go mod tidy
Building and Packages
go run test.go go build test.go
If there are multiple files in the main package, must make go access all of them:
go run . go build .
To remove debug symbols:
go build -ldflags="-s -w" .
In multi-file projects, main.go is the convention. The binary name is taken from the project directory.
gofmt -w . gofmt -d test.go
Initialise project, create go.mod file:
go mod init example-project
Download, and add reference to go.mod file:
go get github.com/alexflint/go-arg
Ensure go.mod is in sync with code imports:
go mod tidy
Note the go.sum file that contains anti-tampering checksums.
To cross compile:
GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o disco_scan_updater GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o disco_scan_updater.exe
Show available platforms:
go tool dist list
Windows Executables with winres
To add icons and version details:
go install github.com/tc-hib/go-winres@latest ~/go/bin/go-winres init
Add resources:
- Edit winres/winres.json (icon reference: RT_GROUP_ICON)
- Add winres/icon.png (s single high resolution PNG will be rescaled automatically
Make syso resources, and build into binary:
~/go/bin/go-winres make go build