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) {
139 Pointers point to a memory location of a variable. Go is fully garbage-collected.
141 See: [Pointers](https://tour.golang.org/moretypes/1)
151 See: [Type conversions](https://tour.golang.org/basics/13)
159 if day == "sunday" || day == "saturday" {
161 } else if day == "monday" && isTired() {
167 {: data-line="1,3,5"}
169 See: [If](https://tour.golang.org/flowcontrol/5)
174 if _, err := getResult(); err != nil {
180 A condition in an `if` statement can be preceded with a statement before a `;`.
182 See: [If with a short statement](https://tour.golang.org/flowcontrol/6)
189 // cases don't "fall through" by default!
200 See: [Switch](https://github.com/golang/go/wiki/Switch)
205 for count := 0; count <= 10; count++ {
206 fmt.Println("My counter is at", count)
210 See: [For loops](https://tour.golang.org/flowcontrol/1)
215 entry := []string{"Jack","John","Jones"}
216 for i, val := range entry {
217 fmt.Printf("At position %d, the character %s is present\n", i, val)
221 See: [For-Range loops](https://gobyexample.com/range)
229 myfunc := func() bool {
235 Functions are first class objects.
237 ### Multiple return types
244 func getMessage() (a string, b string) {
245 return "Hello", "World"
251 ### Named return values
254 func split(sum int) (x, y int) {
262 By defining the return value names in the signature, a `return` (no args) will return variables with those names.
264 See: [Named return values](https://tour.golang.org/basics/7)
278 "fmt" // gives fmt.Println
279 "math/rand" // gives rand.Intn
285 See: [Importing](https://tour.golang.org/basics/1)
306 Exported names begin with capital letters.
308 See: [Exported names](https://tour.golang.org/basics/3)
316 Every package file has to start with `package`.
326 ch := make(chan string)
328 // Start concurrent routines
334 // (Since our goroutines are concurrent,
335 // the order isn't guaranteed!)
336 fmt.Println(<-ch, <-ch, <-ch)
339 {: data-line="3,6,7,8,13"}
342 func push(name string, ch chan string) {
343 msg := "Hey, " + name
349 Channels are concurrency-safe communication objects, used in goroutines.
351 See: [Goroutines](https://tour.golang.org/concurrency/1), [Channels](https://tour.golang.org/concurrency/2)
353 ### Buffered channels
356 ch := make(chan int, 2)
361 // all goroutines are asleep - deadlock!
365 Buffered channels limit the amount of messages it can keep.
367 See: [Buffered channels](https://tour.golang.org/concurrency/3)
371 #### Closes a channel
381 #### Iterates across a channel until its closed
390 #### Closed if `ok == false`
396 See: [Range and close](https://tour.golang.org/concurrency/4)
404 defer fmt.Println("Done")
405 fmt.Println("Working...")
410 Defers running a function until the surrounding function returns.
411 The arguments are evaluated immediately, but the function call is not ran until later.
413 See: [Defer, panic and recover](https://blog.golang.org/defer-panic-and-recover)
415 ### Deferring functions
422 fmt.Println("Working...")
426 {: data-line="2,3,4"}
428 Lambdas are better suited for defer blocks.
433 defer func(d *int64) {
434 fmt.Printf("& %v Unix Sec\n", *d)
437 d = time.Now().Unix()
440 {: data-line="3,4,5"}
441 The defer func uses current value of d, unless we use a pointer to get final value at end of main.
454 {: data-line="1,2,3,4"}
460 fmt.Println(v.X, v.Y)
464 See: [Structs](https://tour.golang.org/moretypes/2)
469 v := Vertex{X: 1, Y: 2}
473 // Field names can be omitted
482 You can also put field names.
484 ### Pointers to structs
491 Doing `v.X` is the same as doing `(*v).X`, when `v` is a pointer.
504 func (v Vertex) Abs() float64 {
505 return math.Sqrt(v.X * v.X + v.Y * v.Y)
515 There are no classes, but you can define functions with _receivers_.
517 See: [Methods](https://tour.golang.org/methods/1)
522 func (v *Vertex) Scale(f float64) {
535 By defining your receiver as a pointer (`*Vertex`), you can do mutations.
537 See: [Pointer receivers](https://tour.golang.org/methods/4)
541 - [A tour of Go](https://tour.golang.org/welcome/1) _(tour.golang.org)_
542 - [Golang wiki](https://github.com/golang/go/wiki/) _(github.com)_
543 - [Awesome Go](https://awesome-go.com/) _(awesome-go.com)_
544 - [Go by Example](https://gobyexample.com/) _(gobyexample.com)_
545 - [Effective Go](https://golang.org/doc/effective_go.html) _(golang.org)_
546 - [JustForFunc Youtube](https://www.youtube.com/channel/UC_BzFbxG2za3bp5NRRRXJSw) _(youtube.com)_
547 - [Style Guide](https://github.com/golang/go/wiki/CodeReviewComments) _(github.com)_