-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathintro_to_go.txt
41 lines (38 loc) · 2.03 KB
/
intro_to_go.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Intro to Go source code and compiling
Let's assume you know what Go is and why you want to use it.
1. Install Go
If using Linux, simply run my Go install script (works on amd/arm 32/64 bit *nix):
https://github.com/cyclone-github/scripts/blob/main/install_go.sh
If you want to manually install Go on Linux / Windows / Mac, follow the official directions here:
https://go.dev/doc/install
Confirm Go is working by running "go version" from terminal / cmd.
2. Run Go Source Code (to test before compiling)
Run these commands from terminal / cmd from the directory that contains your *.go source code.
If “main.go” is your source code file:
go mod init main.go
go mod tidy
For single *.go file:
go run main.go
For multiple *.go files:
go run .
3. Compile Go Source Code
If using Linux, you can use my shell script to download dependencies and x-compile source code for Linux, Windows or Mac.
Take a look at the script and comment out any options you don't need. Once the script has completed, your new binaries will be in the newly created bin/ directory within your current directory.
https://github.com/cyclone-github/scripts/blob/main/go_compiler.sh
To manually compile Go source code, run these commands from terminal / cmd from the directory that contains your *.go source code.
If “main.go” is your source code file:
go mod init main.go
go mod tidy
For single *.go file:
go build main.go
For multiple *.go files:
go build .
>or<
go build -ldflags="-s -w" main.go (-ldflags="-s -w" omits symbol table and debug information, making the binary smaller)
go build -ldflags="-s -w" . <--- for multi-source file program
On linux, I like to run strip to remove debug info & upx to compress the binary. My shell script above automatically does this, but you can run these commands manually as well.
If “main.bin” is your binary file:
strip main.bin
upx main.bin
Note: compressing Go binaries with upx can cause false positives on some anti-virus programs, especially on Windows (exe) binaries.
More info on false positives with Go binaries: https://go.dev/doc/faq#virus