Go cli: Difference between revisions
Line 41: | Line 41: | ||
Go install will do all of what build will do but will also create the bin and the library in the workspace. e.g. | Go install will do all of what build will do but will also create the bin and the library in the workspace. e.g. | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
go install | go install cmds/hello | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[File:Go Cli tree3.png|200px]] | [[File:Go Cli tree3.png|200px]] | ||
==Other Commands== | |||
So here are some other flags | |||
-a force rebuild | |||
-n dry run with verbose | |||
-n no dry run with verbose | |||
-p for processors | |||
-v verbose | |||
==Race Flag== | |||
This is a flag to detect issues with concurrency adding this to the run can help find issues. Just run the code with -race. Without the shadowing to local scope this code would fail. | |||
<syntaxhighlight lang="go"> | |||
package main | |||
import "fmt" | |||
import "sync" | |||
func main() { | |||
var wg sync.WaitGroup | |||
for i := 0; i < 10; i++ { | |||
wg.Add(1) | |||
// ===>>> i := i | |||
go func() { | |||
fmt.Println(i) | |||
wg.Done() | |||
}() | |||
} | |||
wg.Wait() | |||
} | |||
</syntaxhighlight> | |||
Running with -race will help | |||
<syntaxhighlight lang="bash"> | |||
================== | |||
WARNING: DATA RACE | |||
Read at 0x00c0000160b0 by goroutine 7: | |||
main.main.func1() | |||
/home/iwiseman/dev/courses/go-cli/03/demos/demos/03-race/main.go:12 +0x3c | |||
Previous write at 0x00c0000160b0 by main goroutine: | |||
main.main() | |||
/home/iwiseman/dev/courses/go-cli/03/demos/demos/03-race/main.go:8 +0x108 | |||
Goroutine 7 (running) created at: | |||
main.main() | |||
/home/iwiseman/dev/courses/go-cli/03/demos/demos/03-race/main.go:11 +0xe4 | |||
================== | |||
</syntaxhighlight> |
Revision as of 03:01, 30 January 2021
Introduction
This is just a page on go command options
go help <command>
// e.g.
go help test
We can go to https://golang.org/cmd/go/ for online
Building and Running
Environment
For me I have set up my PATH to include go and my GOROOT to be the parent directory of the src folder.
export PATH=/usr/local/go/bin:$PATH
cd blah1/blah2/src/..
export GOPATH=`pwd`
Run
Compiles and run a program without leaving artefacts.
go run src/cmds/hello/hello.go
Build
Compiles but does not run a program. Given the tree
This command will build the library but not produce anything.
go build hello
This command will build the binary and produce a binary
go build cmds/hello
Adding the -i forces the intermediate files to be created on disk in the pkg directory
go build -i cmds/hello
Install
Go install will do all of what build will do but will also create the bin and the library in the workspace. e.g.
go install cmds/hello
Other Commands
So here are some other flags
-a force rebuild -n dry run with verbose -n no dry run with verbose -p for processors -v verbose
Race Flag
This is a flag to detect issues with concurrency adding this to the run can help find issues. Just run the code with -race. Without the shadowing to local scope this code would fail.
package main
import "fmt"
import "sync"
func main() {
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
// ===>>> i := i
go func() {
fmt.Println(i)
wg.Done()
}()
}
wg.Wait()
}
Running with -race will help
==================
WARNING: DATA RACE
Read at 0x00c0000160b0 by goroutine 7:
main.main.func1()
/home/iwiseman/dev/courses/go-cli/03/demos/demos/03-race/main.go:12 +0x3c
Previous write at 0x00c0000160b0 by main goroutine:
main.main()
/home/iwiseman/dev/courses/go-cli/03/demos/demos/03-race/main.go:8 +0x108
Goroutine 7 (running) created at:
main.main()
/home/iwiseman/dev/courses/go-cli/03/demos/demos/03-race/main.go:11 +0xe4
==================