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}
### 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)
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}
for _, item := range itemList {
// Increment WaitGroup Counter
wg.Add(1)
- go doOperation(item)
+ go doOperation(&wg, item)
}
// Wait for goroutines to finish
wg.Wait()
{: data-line="1,4,8,12"}
```go
-func doOperation(item string) {
+func doOperation(wg *sync.WaitGroup, item string) {
defer wg.Done()
// do operation on item
// ...
fmt.Println("Working...")
}
```
-
{: data-line="2,3,4"}
Lambdas are better suited for defer blocks.
{: 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)_