4 prism_languages: [go, bash]
26 message := greetMe("world")
30 func greetMe(name string) string {
31 return "Hello, " + name + "!"
39 Or try it out in the [Go repl](https://repl.it/languages/go), or [A Tour of Go](https://tour.golang.org/welcome/1).
43 #### Variable declaration
50 #### Shortcut of above (Infers type)
62 Constants can be character, string, boolean, or numeric values.
64 See: [Constants](https://tour.golang.org/basics/15)
80 Strings are of type `string`.
89 num := 3 + 4i // complex128
90 num := byte('a') // byte (alias for uint8)
96 var u uint = 7 // uint (unsigned)
97 var p float32 = 22.7 // 32-bit float
103 // var numbers [5]int
104 numbers := [...]int{0, 0, 0, 0, 0}
107 Arrays have a fixed size.
112 slice := []int{2, 3, 4}
116 slice := []byte("Hello")
119 Slices have a dynamic size, unlike arrays.
126 fmt.Println("Value is", b)
132 func getPointer () (myPointer *int) {
145 Pointers point to a memory location of a variable. Go is fully garbage-collected.
147 See: [Pointers](https://tour.golang.org/moretypes/1)
157 See: [Type conversions](https://tour.golang.org/basics/13)
165 if day == "sunday" || day == "saturday" {
167 } else if day == "monday" && isTired() {
173 {: data-line="1,3,5"}
175 See: [If](https://tour.golang.org/flowcontrol/5)
180 if _, err := getResult(); err != nil {
186 A condition in an `if` statement can be preceded with a statement before a `;`.
188 See: [If with a short statement](https://tour.golang.org/flowcontrol/6)
195 // cases don't "fall through" by default!
206 See: [Switch](https://github.com/golang/go/wiki/Switch)
211 for count := 0; count <= 10; count++ {
212 fmt.Println("My counter is at", count)
216 See: [For loops](https://tour.golang.org/flowcontrol/1)
221 entry := []string{"Jack","John","Jones"}
222 for i, val := range entry {
223 fmt.Printf("At position %d, the character %s is present\n", i, val)
227 See: [For-Range loops](https://gobyexample.com/range)
235 myfunc := func() bool {
241 Functions are first class objects.
243 ### Multiple return types
250 func getMessage() (a string, b string) {
251 return "Hello", "World"
257 ### Named return values
260 func split(sum int) (x, y int) {
268 By defining the return value names in the signature, a `return` (no args) will return variables with those names.
270 See: [Named return values](https://tour.golang.org/basics/7)
284 "fmt" // gives fmt.Println
285 "math/rand" // gives rand.Intn
291 See: [Importing](https://tour.golang.org/basics/1)
312 Exported names begin with capital letters.
314 See: [Exported names](https://tour.golang.org/basics/3)
322 Every package file has to start with `package`.
332 ch := make(chan string)
334 // Start concurrent routines
340 // (Since our goroutines are concurrent,
341 // the order isn't guaranteed!)
342 fmt.Println(<-ch, <-ch, <-ch)
345 {: data-line="3,6,7,8,13"}
348 func push(name string, ch chan string) {
349 msg := "Hey, " + name
355 Channels are concurrency-safe communication objects, used in goroutines.
357 See: [Goroutines](https://tour.golang.org/concurrency/1), [Channels](https://tour.golang.org/concurrency/2)
359 ### Buffered channels
362 ch := make(chan int, 2)
367 // all goroutines are asleep - deadlock!
371 Buffered channels limit the amount of messages it can keep.
373 See: [Buffered channels](https://tour.golang.org/concurrency/3)
377 #### Closes a channel
387 #### Iterates across a channel until its closed
396 #### Closed if `ok == false`
402 See: [Range and close](https://tour.golang.org/concurrency/4)
410 var wg sync.WaitGroup
412 for _, item := range itemList {
413 // Increment WaitGroup Counter
417 // Wait for goroutines to finish
422 {: data-line="1,4,8,12"}
425 func doOperation(item string) {
427 // do operation on item
433 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.
434 See: [WaitGroup](https://golang.org/pkg/sync/#WaitGroup)
443 defer fmt.Println("Done")
444 fmt.Println("Working...")
449 Defers running a function until the surrounding function returns.
450 The arguments are evaluated immediately, but the function call is not ran until later.
452 See: [Defer, panic and recover](https://blog.golang.org/defer-panic-and-recover)
454 ### Deferring functions
461 fmt.Println("Working...")
464 {: data-line="2,3,4"}
466 Lambdas are better suited for defer blocks.
471 defer func(d *int64) {
472 fmt.Printf("& %v Unix Sec\n", *d)
475 d = time.Now().Unix()
478 {: data-line="3,4,5"}
479 The defer func uses current value of d, unless we use a pointer to get final value at end of main.
492 {: data-line="1,2,3,4"}
498 fmt.Println(v.X, v.Y)
502 See: [Structs](https://tour.golang.org/moretypes/2)
507 v := Vertex{X: 1, Y: 2}
511 // Field names can be omitted
520 You can also put field names.
522 ### Pointers to structs
529 Doing `v.X` is the same as doing `(*v).X`, when `v` is a pointer.
542 func (v Vertex) Abs() float64 {
543 return math.Sqrt(v.X * v.X + v.Y * v.Y)
553 There are no classes, but you can define functions with _receivers_.
555 See: [Methods](https://tour.golang.org/methods/1)
560 func (v *Vertex) Scale(f float64) {
573 By defining your receiver as a pointer (`*Vertex`), you can do mutations.
575 See: [Pointer receivers](https://tour.golang.org/methods/4)
579 - [A tour of Go](https://tour.golang.org/welcome/1) _(tour.golang.org)_
580 - [Golang wiki](https://github.com/golang/go/wiki/) _(github.com)_
581 - [Awesome Go](https://awesome-go.com/) _(awesome-go.com)_
582 - [Go by Example](https://gobyexample.com/) _(gobyexample.com)_
583 - [Effective Go](https://golang.org/doc/effective_go.html) _(golang.org)_
584 - [JustForFunc Youtube](https://www.youtube.com/channel/UC_BzFbxG2za3bp5NRRRXJSw) _(youtube.com)_
585 - [Style Guide](https://github.com/golang/go/wiki/CodeReviewComments) _(github.com)_