Go examples ⬆
Directory examples\ contains Go code examples coming from various websites - mostly from the Go project. |
This example has the following directory structure :
> tree /a /f . | findstr /v /b [A-Z] | build.bat | go.mod | magefile.go \---src \---main basics.go
Command build.bat
executes the Go command %GOROOT%\bin\go build
with the appropriate parameters and runs the generated executable basics.exe
1:
> build -verbose run Compile 1 Go source file to directory "target" Execute target "target\basics.exe" My favorite number is 2 3.141592653589793 55 world hello 7 10 2 true false no! Type: bool Value: false Type: uint64 Value: 18446744073709551615 Type: complex128 Value: (2+3i) Type: int32 Value: 8658 Hello 世界 Happy 3.14 Day Go rules? true
hello
Example ▴
This example has the following directory structure :
> tree /a /f . | findstr /v /b [A-Z] | build.bat | go.mod | magefile.go | Makefile \---src +---main | hello.go \---test hello_test.go
Command build.bat
executes the Go command %GOROOT%\bin\go build
with the appropriate parameters and runs the generated executable hello.exe
:
> build -verbose run Compile 1 Go source files to directory "target" Execute target "target\hello.exe" hello, world
Build tool mage.exe
takes its Makefile-like runnable targets from the Go file mage.go
; for instance targets clean
and build
:
Note : Mage supports a makefile-style tree of dependencies using the helper library
github.com/magefile/mage/mg
. To declare dependencies, pass any number of dependent functions tomg.Deps
.
> mage clean build & target\hello.exe hello, world
Adding the Mage option -v
will print some progress messages :
> mage -v clean build & target\hello.exe Running target: Clean Running target: Build hello, world
Finally, one may also invoke the Mage target run
directly (target run
requires at least one argument, thus the dummy argument 0
below):
> mage clean run 0 hello, world
WIP
Footnotes ▴
[1] Code disassembling ↩
-
The Go command
objdump
prints out the disassembled binary code for the generated executable, e.g.target\basics.exe
; here is an excerpt of themain
function (source:basics.go
): -
> go tool objdump -S target\basics.exe | awk 'BEGIN{n=0}/basics/{n=1}{if (n^>0) print $0}' TEXT main.main(SB) K:/examples/basics/src/main/basics.go func main() { 0x49a800 4c8da42490feffff LEAQ 0xfffffe90(SP), R12 0x49a808 4d3b6610 CMPQ R12, 0x10(R14) 0x49a80c 0f862d060000 JBE 0x49ae3f 0x49a812 55 PUSHQ BP 0x49a813 4889e5 MOVQ SP, BP 0x49a816 4881ece8010000 SUBQ $0x1e8, SP fmt.Println("My favorite number is", rand.Intn(10)) [...]
fmt.Println("Go rules?", Truth) 0x49adc5 440f11bc2498000000 MOVUPS X15, 0x98(SP) 0x49adce 440f11bc24a8000000 MOVUPS X15, 0xa8(SP) 0x49add7 488d0da2950000 LEAQ type:*+37760(SB), CX 0x49adde 48898c2498000000 MOVQ CX, 0x98(SP) 0x49ade6 488d0dd3890400 LEAQ runtime.buildVersion.str+96(SB), CX 0x49aded 48898c24a0000000 MOVQ CX, 0xa0(SP) 0x49adf5 488d0d84990000 LEAQ type:*+38784(SB), CX 0x49adfc 48898c24a8000000 MOVQ CX, 0xa8(SP) 0x49ae04 488d0d7d370c00 LEAQ runtime.staticuint64s+8(SB), CX 0x49ae0b 48898c24b0000000 MOVQ CX, 0xb0(SP)return Fprintln(os.Stdout, a...) 0x49ae13 488b1d76f70c00 MOVQ os.Stdout(SB), BX 0x49ae1a 488d05778f0400 LEAQ go:itab.*os.File,io.Writer(SB), AX 0x49ae21 488d8c2498000000 LEAQ 0x98(SP), CX 0x49ae29 bf02000000 MOVL $0x2, DI 0x49ae2e 4889fe MOVQ DI, SI 0x49ae31 e80a83ffff CALL fmt.Fprintln(SB) } 0x49ae36 4881c4e8010000 ADDQ $0x1e8, SP 0x49ae3d 5d POPQ BP 0x49ae3e c3 RET func main() { 0x49ae3f 90 NOPL 0x49ae40 e8fbeffcff CALL runtime.morestack_noctxt.abi0(SB) 0x49ae45 e9b6f9ffff JMP main.main(SB)lensm
– a Go assembly and source viewer – is a nice alternative to view both assembly and source (see post "Easy to read Golang assembly output?" on stackoverflow)> go install loov.dev/lensm@main go: downloading loov.dev/lensm v0.0.4 go: downloading gioui.org v0.3.1 go: downloading golang.org/x/exp/shiny v0.0.0-20230713183714-613f0c0eb8a1 go: downloading github.com/go-text/typesetting v0.0.0-20230803102845-24e03d8b5372 go: downloading golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 go: downloading golang.org/x/image v0.5.0 go: downloading golang.org/x/text v0.7.0 go: downloading github.com/tetratelabs/wabin v0.0.0-20230304001439-f6f874872834 go: downloading golang.org/x/sys v0.1.0 go: downloading gioui.org/cpu v0.0.0-20210817075930-8d6a761490d2 go: downloading gioui.org/shader v1.0.8 go: downloading golang.org/x/arch v0.2.0 > where lensm %USERPROFILE%\go\bin\lensm.exe
The following screenshot presents the same assembly code as above :
> lensm -filter "main" target\basics.exe