Skip to content

Commit fcc38f3

Browse files
committed
Added Go notes WIP
1 parent 4145dd9 commit fcc38f3

File tree

3 files changed

+292
-0
lines changed

3 files changed

+292
-0
lines changed

go/learn_go.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Learn Go
2+
3+
Let's _go_
4+
5+
```go
6+
// helloworld.go
7+
package main
8+
9+
import "fmt"
10+
11+
func main() {
12+
fmt.Println("Hello, World!")
13+
}
14+
```
15+
16+
```bash
17+
$ go run helloworld.go
18+
Hello, World!
19+
```
20+
21+
Or with an explicit build step:
22+
23+
```bash
24+
$ go build helloworld.go
25+
$ ./helloworld
26+
Hello, World!
27+
```
28+
29+
A variantion of this implementation of [hello world](https://en.wikipedia.org/wiki/%22Hello%2C_World!%22_program), can be found at [the official Go website][goorg].
30+
31+
I can recommend taking [the tour of Go][gotour], which gives a brief overview of the Go language and this brief introduction on [how to write Go code][gohowto] will really get you going.
32+
33+
Anyway lets break down our example and process for the above example.
34+
35+
**Line 1**: is a comment, I have just put the suggested file name for later use
36+
37+
**Line 2**: a Go file should always contain a package
38+
39+
**Line 3**: Whitespace is insignificant, but improves readability if used widely, this is however not Go specific
40+
41+
**Line 4**: We import [the package `fmt`](https://golang.org/pkg/fmt/), pronounced _fumpt_
42+
43+
We can import all the packages we need, the recommendation for importing several packages is the following construct:
44+
45+
```go
46+
import (
47+
"fmt"
48+
"math"
49+
)
50+
```
51+
52+
Actually tools like `golint` will help you out with this automatically, but let us save that for later.
53+
54+
**Line 6**: This is function declaration
55+
56+
The `main` function, is indication of where there executable should start.
57+
58+
No parameters:
59+
60+
```go
61+
func main() {...}
62+
```
63+
64+
One parameter:
65+
66+
```go
67+
func main(number int) {}
68+
```
69+
70+
Two parameters and a return value
71+
72+
```go
73+
fun main(operand1, operand2 int) int {}
74+
```
75+
76+
Two parameters and a two return values
77+
78+
```go
79+
fun main(operand1, operand2 int) (int, int) {}
80+
```
81+
82+
There are more variations to function declarations, do checkout the documentation or a cheatsheet.
83+
84+
Anyway to set us back a bit, some would say that we did this the wrong way around, we should have written tests first.
85+
86+
So lets add some tests, so make sure our "hello world" implementation works as expected.
87+
88+
This requires that we dwelve into Go modules and Go testing.
89+
90+
Now that you somewhat up and running with Go, some next steps could be:
91+
92+
- Check out a proposal for a [Go Developer Roadmap][godevroadmap2020] for learning Go in 2020
93+
- Or you could learn fundamental [Algorithms with Go][algo]
94+
- IF you like me is a complete _n00b_ consider following the [Go track on Exercism.io](https://exercism.io/tracks/go) or [the katas on Codewars.com](https://www.codewars.com/?language=go)
95+
96+
I will continue my journey and added more TILs on different aspects of Go programming and learning Go - good luck and have fun...
97+
98+
## References
99+
100+
- [A tour of Go][gotour]
101+
- [How to write Go code](https://golang.org/doc/code.html)
102+
- [Effective Go][gohowto]
103+
- [Go go-to guide](https://yourbasic.org/golang/)
104+
- [Official Go Website][goorg]
105+
106+
- [Go Developer Roadmap 2020](godevroadmap2020)
107+
- [Algorithms with Go][algo]
108+
109+
- [StackOverflow: "Go: Meaning of the 'fmt' package acronym"](https://stackoverflow.com/questions/23597165/go-meaning-of-the-fmt-package-acronym)
110+
111+
[goorg]: https://golang.org/
112+
[gotour]: https://tour.golang.org/welcome/1
113+
[gohowto]: https://golang.org/doc/code.html
114+
[algo]: https://algorithmswithgo.com/
115+
[godevroadmap2020]: https://github.com/Alikhll/golang-developer-roadmap

go/reading_benchmark_results.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Reading benchmark results
2+
3+
Go has benchmarking capabilities as part of it's standard toolchain.
4+
5+
If you want to do a benchmark of your current Go programming project:
6+
7+
```bash
8+
$ go test -bench . -count 3
9+
goos: darwin
10+
goarch: amd64
11+
pkg: twofer
12+
BenchmarkShareWith-8 14216751 81.7 ns/op
13+
BenchmarkShareWith-8 13949208 81.1 ns/op
14+
BenchmarkShareWith-8 14090535 82.4 ns/op
15+
PASS
16+
ok twofer 3.769s
17+
```
18+
19+
_NB: I have condensed the output a bit for readability, by collapsing excessive whitespace_.
20+
21+
It does require that the test suite has a benchmarking function.
22+
23+
This runs our test suite and benchmarks it, with 3 iterations, specified by the `-count` parameter.
24+
25+
As you can read from the example output.
26+
27+
- 3 iterations are run
28+
- First column is the test run, which points to a function in the `two_fer_test.go`
29+
- Second column is the amount of times the test was run
30+
- Third column is the measured time and yes `ns` is [nanoseconds](https://en.wikipedia.org/wiki/Nanosecond)
31+
32+
We both specify a number of iterations, but the test suite does an internal count, I have quoted the explanation from [the official documentation][pkgtesting]:
33+
34+
> The benchmark function must run the target code b.N times. During benchmark execution, b.N is
35+
> adjusted until the benchmark function lasts long enough to be timed reliably.
36+
37+
Please see the official documentation for more information.
38+
39+
Now if you want even more detail, you can also get numbers on memory allocation using the `-benchmem` flag. There are plenty of flags, [check the official documentation][testingflags] for a listing.
40+
41+
```bash
42+
$ go test -bench . -benchmem -count 3
43+
goos: darwin
44+
goarch: amd64
45+
pkg: twofer
46+
BenchmarkShareWith-8 14013910 82.4 ns/op 0 B/op 0 allocs/op
47+
BenchmarkShareWith-8 14102596 81.7 ns/op 0 B/op 0 allocs/op
48+
BenchmarkShareWith-8 14164332 81.1 ns/op 0 B/op 0 allocs/op
49+
PASS
50+
ok twofer 3.873s
51+
```
52+
53+
_NB: I have condensed the output a bit for readability, by collapsing excessive whitespace_.
54+
55+
This runs our test suite again and benchmarks it, with 3 iterations, but outputs more information.
56+
57+
_Since I was unable to understand the output, I had to ask around and I received the following explanation_. Thanks to **mzi** from the Gopher slack for the explanation.
58+
59+
> B/op is how many bytes were allocated per iteration, and allocs/op is how many allocations were
60+
> made. (per iteration)
61+
62+
The first 3 columns are the ones from the basic benchmark. So with the `--benchmem` flag, two more columns are added:
63+
64+
- Fourth column is the number of _extra_ bytes allocated per iteration
65+
- Fifth column is the number of _extra_ allocations made per iteration
66+
67+
And finally thanks to **superstas** at Exercism.io for pushing me into benchmarking my first real Go solution.
68+
69+
To describe more of what I learned, lets examine my solution from [Exercism.io][Exercism.io].
70+
71+
```go
72+
package twofer
73+
74+
func ShareWith(name string) string {
75+
if name == "" {
76+
name = "you"
77+
}
78+
79+
return "One for " + name + ", one for me."
80+
}
81+
```
82+
83+
_NB: I have condensed the output a bit for readability, by removing inline documentation_.
84+
85+
This was the optimized solution, as pointed out by **superstas** could be made simpler and less expernsive resource wise.
86+
87+
The solution proposal he was giving feedback on was this one:
88+
89+
```go
90+
package twofer
91+
92+
import "fmt"
93+
94+
func ShareWith(name string) string {
95+
if name == "" {
96+
name = "you"
97+
}
98+
99+
return fmt.Sprintf("One for %s, one for me.", name)
100+
}
101+
```
102+
103+
_NB: I have condensed the output a bit for readability, by removing inline documentation_.
104+
105+
If we benchmark this you can compare the numbers:
106+
107+
```bash
108+
$ go test -bench . -benchmem -count 3
109+
goos: darwin
110+
goarch: amd64
111+
pkg: twofer
112+
BenchmarkShareWith-8 2691262 443 ns/op 144 B/op 6 allocs/op
113+
BenchmarkShareWith-8 2716256 443 ns/op 144 B/op 6 allocs/op
114+
BenchmarkShareWith-8 2702143 447 ns/op 144 B/op 6 allocs/op
115+
PASS
116+
ok twofer 5.207s
117+
```
118+
119+
_NB: I have condensed the output a bit for readability, by collapsing excessive whitespace_.
120+
121+
As pointed out by **superstas** use of Go's `fmt` package comes at a certain price and in this case a basic concatenation would suffice as demonstrated in the example with the optimized solution.
122+
123+
With Go benchmarking at the tip of your fingers, you can gain insights on your code, read the documentation and start experimenting and perhaps even optimizing.
124+
125+
## References
126+
127+
- [Exercism.io](https://exercism.io/)
128+
- [My exercism.io "two-fer" solution](https://exercism.io/tracks/go/exercises/two-fer/solutions/670e02e265634d3ab83821b0649f83c7)
129+
- [Gophers Slack](https://invite.slack.golangbridge.org/)
130+
- [Go package: "testing"][pkgtesting]
131+
- [Go command: Testing flags][testingflags]
132+
133+
[pkgtesting]: https://golang.org/pkg/testing/
134+
[testingflags]: https://golang.org/cmd/go/#hdr-Testing_flags

go/resources.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Go Resources
2+
3+
This is the output of the bot helping out in the [Gopher Slack][gopherslack] for learning resources etc.
4+
5+
```
6+
Here are some resources you should check out if you are learning / new to Go:
7+
First you should take the language tour: https://tour.golang.org/
8+
9+
Then, you should visit:
10+
11+
- https://golang.org/doc/code.html to learn how to organize your Go workspace
12+
- https://golang.org/doc/effective_go.html be more effective at writing Go
13+
- https://golang.org/ref/spec learn more about the language itself
14+
- https://golang.org/doc/#articles a lot more reading material
15+
16+
There are some awesome websites as well:
17+
18+
- https://blog.gopheracademy.com great resources for Gophers in general
19+
- http://gotime.fm awesome weekly podcast of Go awesomeness
20+
- https://gobyexample.com examples of how to do things in Go
21+
- http://go-database-sql.org how to use SQL databases in Go
22+
- https://dmitri.shuralyov.com/idiomatic-go tips on how to write more idiomatic Go code
23+
- https://divan.github.io/posts/avoid_gotchas will help you avoid gotchas in Go
24+
- https://golangbot.com tutorials to help you get started in Go
25+
26+
There's also an exhaustive list of videos http://gophervids.appspot.com related to Go from various authors.
27+
28+
If you prefer books, you can try these:
29+
30+
- http://www.golangbootcamp.com/book
31+
- http://gopl.io/
32+
- https://www.manning.com/books/go-in-action (if you e-mail @wkennedy at bill@ardanlabs.com you can get a free copy for being part of this Slack)
33+
34+
If you want to learn how to organize your Go project, make sure to read: https://medium.com/@benbjohnson/standard-package-layout-7cdbc8391fc1#.ds38va3pp.
35+
Once you are accustomed to the language and syntax, you can read this series of articles for a walkthrough the various standard library packages: https://medium.com/go-walkthrough.
36+
Finally, https://github.com/golang/go/wiki#learning-more-about-go will give a list of even more resources to learn Go
37+
```
38+
39+
## References
40+
41+
- [Gopher Slack][gopherslack]
42+
43+
[gopherslack]: https://invite.slack.golangbridge.org/

0 commit comments

Comments
 (0)