weight: -3
tags: [Featured]
category: C-like
-updated: 2017-09-15
+updated: 2020-06-21
---
## Getting started
{: .-three-column}
+### Introduction
+{: .-intro}
+
+- [A tour of Go](https://tour.golang.org/welcome/1) _(tour.golang.org)_
+- [Go repl](https://repl.it/languages/go) _(repl.it)_
+- [Golang wiki](https://github.com/golang/go/wiki/) _(github.com)_
+
### Hello world
{: .-prime}
```
{: data-line="3"}
+```go
+a := new(int)
+*a = 234
+```
+{: data-line="2"}
+
Pointers point to a memory location of a variable. Go is fully garbage-collected.
See: [Pointers](https://tour.golang.org/moretypes/1)
### Statements in if
```go
-if _, err := getResult(); err != nil {
+if _, err := doThing(); err != nil {
fmt.Println("Uh oh")
}
```
{: data-line="1"}
-A condition in an `if` statement can be preceded with a statement before a `;`.
+A condition in an `if` statement can be preceded with a statement before a `;`. Variables declared by the statement are only in scope until the end of the `if`.
See: [If with a short statement](https://tour.golang.org/flowcontrol/6)
### For loop
```go
- for count := 0; count <= 10; count++ {
- fmt.Println("My counter is at", count)
- }
+for count := 0; count <= 10; count++ {
+ fmt.Println("My counter is at", count)
+}
```
See: [For loops](https://tour.golang.org/flowcontrol/1)
### For-Range loop
```go
- entry := []string{"Jack","John","Jones"}
- for i, val := range entry {
- fmt.Printf("At position %d, the character %s is present\n", i, val)
- }
+entry := []string{"Jack","John","Jones"}
+for i, val := range entry {
+ fmt.Printf("At position %d, the character %s is present\n", i, val)
+}
```
See: [For-Range loops](https://gobyexample.com/range)
+### While loop
+
+```go
+n := 0
+x := 42
+for n != x {
+ n := guess()
+}
+```
+
+See: [Go's "while"](https://tour.golang.org/flowcontrol/3)
+
## Functions
{: .-three-column}
See: [Range and close](https://tour.golang.org/concurrency/4)
+### WaitGroup
+
+```go
+import "sync"
+
+func main() {
+ var wg sync.WaitGroup
+
+ for _, item := range itemList {
+ // Increment WaitGroup Counter
+ wg.Add(1)
+ go doOperation(&wg, item)
+ }
+ // Wait for goroutines to finish
+ wg.Wait()
+
+}
+```
+{: data-line="1,4,8,12"}
+
+```go
+func doOperation(wg *sync.WaitGroup, item string) {
+ defer wg.Done()
+ // do operation on item
+ // ...
+}
+```
+{: data-line="2"}
+
+A WaitGroup waits for a collection of goroutines to finish. The main goroutine calls Add to set the number of goroutines to wait for. The goroutine calls `wg.Done()` when it finishes.
+See: [WaitGroup](https://golang.org/pkg/sync/#WaitGroup)
+
+
## Error control
### Defer
Lambdas are better suited for defer blocks.
+```go
+func main() {
+ var d = int64(0)
+ defer func(d *int64) {
+ fmt.Printf("& %v Unix Sec\n", *d)
+ }(&d)
+ fmt.Print("Done ")
+ d = time.Now().Unix()
+}
+```
+{: data-line="3,4,5"}
+The defer func uses current value of d, unless we use a pointer to get final value at end of main.
+
## Structs
{: .-three-column}
{: data-line="1"}
```go
-v: = Vertex{1, 2}
+v := Vertex{1, 2}
v.Abs()
```
See: [Pointer receivers](https://tour.golang.org/methods/4)
+## Interfaces
+
+### A basic interface
+
+```go
+type Shape interface {
+ Area() float64
+ Perimeter() float64
+}
+```
+
+### Struct
+
+```go
+type Rectangle struct {
+ Length, Width float64
+}
+```
+
+Struct `Rectangle` implicitly implements interface `Shape` by implementing all of its methods.
+
+### Methods
+
+```go
+func (r Rectangle) Area() float64 {
+ return r.Length * r.Width
+}
+
+func (r Rectangle) Perimeter() float64 {
+ return 2 * (r.Length + r.Width)
+}
+```
+
+The methods defined in `Shape` are implemented in `Rectangle`.
+
+### Interface example
+
+```go
+func main() {
+ var r Shape = Rectangle{Length: 3, Width: 4}
+ fmt.Printf("Type of r: %T, Area: %v, Perimeter: %v.", r, r.Area(), r.Perimeter())
+}
+```
+
## References
+### Official resources
+{: .-intro}
+
- [A tour of Go](https://tour.golang.org/welcome/1) _(tour.golang.org)_
- [Golang wiki](https://github.com/golang/go/wiki/) _(github.com)_
-- [Awesome Go](https://awesome-go.com/) _(awesome-go.com)_
-- [Go by Example](https://gobyexample.com/) _(gobyexample.com)_
- [Effective Go](https://golang.org/doc/effective_go.html) _(golang.org)_
+
+### Other links
+{: .-intro}
+
+- [Go by Example](https://gobyexample.com/) _(gobyexample.com)_
+- [Awesome Go](https://awesome-go.com/) _(awesome-go.com)_
- [JustForFunc Youtube](https://www.youtube.com/channel/UC_BzFbxG2za3bp5NRRRXJSw) _(youtube.com)_
- [Style Guide](https://github.com/golang/go/wiki/CodeReviewComments) _(github.com)_