forked from farhadmpr/gosuccinctly
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c67ab13
Showing
38 changed files
with
1,153 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Go Succinctly | ||
|
||
This is the companion repo for [*Go Succinctly*](https://github.com/Gommunity/gosuccinctly) by Amir Irani. | ||
|
||
[](https://github.com/Gommunity/gosuccinctly) | ||
|
||
## [*Download pdf*](https://github.com/Gommunity/gosuccinctly/blob/master/Go-Succinctly.pdf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Code listing 1: https://play.golang.org/p/2C7wwJ6nxG | ||
|
||
// Our first program will print the classic "hello world" | ||
// message. Here's the full source code. | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
fmt.Println("hello world") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Code listing 10: https://play.golang.org/p/Lu01QP5AOri | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
myArray := [...]string{"Apples", "Oranges", "Bananas"} | ||
fmt.Printf("Initial array values: %v\n", myArray) | ||
myFunction(myArray) | ||
fmt.Printf("Final array values: %v\n", myArray) | ||
} | ||
|
||
func myFunction(arr [3]string) { | ||
// Change Oranges to Strawberries | ||
arr[1] = "Strawberries" | ||
fmt.Printf("Array values in myFunction(): %v\n", arr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Code listing 11: https://play.golang.org/p/Z3_U32sn8RF | ||
|
||
// _Slices_ are a key data type in Go, giving a more | ||
// powerful interface to sequences than arrays. | ||
|
||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
|
||
// Unlike arrays, slices are typed only by the | ||
// elements they contain (not the number of elements). | ||
// To create an empty slice with non-zero length, use | ||
// the builtin `make`. Here we make a slice of | ||
// `string`s of length `3` (initially zero-valued). | ||
s := make([]string, 3) | ||
fmt.Println("emp:", s) | ||
|
||
// We can set and get just like with arrays. | ||
s[0] = "a" | ||
s[1] = "b" | ||
s[2] = "c" | ||
fmt.Println("set:", s) | ||
fmt.Println("get:", s[2]) | ||
|
||
// `len` returns the length of the slice as expected. | ||
fmt.Println("len:", len(s)) | ||
|
||
// In addition to these basic operations, slices | ||
// support several more that make them richer than | ||
// arrays. One is the builtin `append`, which | ||
// returns a slice containing one or more new values. | ||
// Note that we need to accept a return value from | ||
// `append` as we may get a new slice value. | ||
s = append(s, "d") | ||
s = append(s, "e", "f") | ||
fmt.Println("apd:", s) | ||
|
||
// Slices can also be `copy`'d. Here we create an | ||
// empty slice `c` of the same length as `s` and copy | ||
// into `c` from `s`. | ||
c := make([]string, len(s)) | ||
copy(c, s) | ||
fmt.Println("cpy:", c) | ||
|
||
// Slices support a "slice" operator with the syntax | ||
// `slice[low:high]`. For example, this gets a slice | ||
// of the elements `s[2]`, `s[3]`, and `s[4]`. | ||
l := s[2:5] | ||
fmt.Println("sl1:", l) | ||
|
||
// This slices up to (but excluding) `s[5]`. | ||
l = s[:5] | ||
fmt.Println("sl2:", l) | ||
|
||
// And this slices up from (and including) `s[2]`. | ||
l = s[2:] | ||
fmt.Println("sl3:", l) | ||
|
||
// We can declare and initialize a variable for slice | ||
// in a single line as well. | ||
t := []string{"g", "h", "i"} | ||
fmt.Println("dcl:", t) | ||
|
||
// Slices can be composed into multi-dimensional data | ||
// structures. The length of the inner slices can | ||
// vary, unlike with multi-dimensional arrays. | ||
twoD := make([][]int, 3) | ||
for i := 0; i < 3; i++ { | ||
innerLen := i + 1 | ||
twoD[i] = make([]int, innerLen) | ||
for j := 0; j < innerLen; j++ { | ||
twoD[i][j] = i + j | ||
} | ||
} | ||
fmt.Println("2d: ", twoD) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Code listing 12: https://play.golang.org/p/kHqSobJC2Yv | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
mySlice := []string{"Apples", "Oranges", "Bananas"} | ||
fmt.Printf("Initial slice values: %v\n", mySlice) | ||
myFunction(mySlice) | ||
fmt.Printf("Final slice values: %v\n", mySlice) | ||
} | ||
|
||
func myFunction(fruits []string) { | ||
// Change Oranges to Strawberries | ||
fruits[1] = "Strawberries" | ||
fmt.Printf("Slice values in myFunction(): %v\n", fruits) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Code listing 13: https://play.golang.org/p/p3ewucCUFer | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
mySlice := make([]int, 4, 8) | ||
fmt.Printf("Initial Length: %d\n", len(mySlice)) | ||
fmt.Printf("Capacity: %d\n", cap(mySlice)) | ||
fmt.Printf("Contents: %v\n", mySlice) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Code listing: https://play.golang.org/p/BtiZKrqyImq | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
|
||
mySlice := make([]int, 0, 8) | ||
mySlice = append(mySlice, 1, 3, 5, 7, 9, 11, 13, 17) | ||
|
||
fmt.Printf("Contents: %v\n", mySlice) | ||
fmt.Printf("Number of Items: %d\n", len(mySlice)) | ||
fmt.Printf("Capacity: %d\n", cap(mySlice)) | ||
|
||
mySlice[8] = 19 | ||
|
||
fmt.Printf("Contents: %v\n", mySlice) | ||
fmt.Printf("Number of Items: %d\n", len(mySlice)) | ||
fmt.Printf("Capacity: %d\n", cap(mySlice)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Code listing 15: https://play.golang.org/p/yE-1Rhccebv | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
|
||
mySlice := make([]int, 0, 8) | ||
mySlice = append(mySlice, 1, 3, 5, 7, 9, 11, 13, 17) | ||
|
||
fmt.Printf("Contents: %v\n", mySlice) | ||
fmt.Printf("Number of Items: %d\n", len(mySlice)) | ||
fmt.Printf("Capacity: %d\n", cap(mySlice)) | ||
|
||
mySlice = append(mySlice, 19) | ||
|
||
fmt.Printf("Contents: %v\n", mySlice) | ||
fmt.Printf("Number of Items: %d\n", len(mySlice)) | ||
fmt.Printf("Capacity: %d\n", cap(mySlice)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Code listing 16: https://play.golang.org/p/U67R66Oab8r | ||
|
||
// _Maps_ are Go's built-in [associative data type](http://en.wikipedia.org/wiki/Associative_array) | ||
// (sometimes called _hashes_ or _dicts_ in other languages). | ||
|
||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
|
||
// To create an empty map, use the builtin `make`: | ||
// `make(map[key-type]val-type)`. | ||
m := make(map[string]int) | ||
|
||
// Set key/value pairs using typical `name[key] = val` | ||
// syntax. | ||
m["k1"] = 7 | ||
m["k2"] = 13 | ||
|
||
// Printing a map with e.g. `fmt.Println` will show all of | ||
// its key/value pairs. | ||
fmt.Println("map:", m) | ||
|
||
// Get a value for a key with `name[key]`. | ||
v1 := m["k1"] | ||
fmt.Println("v1: ", v1) | ||
|
||
// The builtin `len` returns the number of key/value | ||
// pairs when called on a map. | ||
fmt.Println("len:", len(m)) | ||
|
||
// The builtin `delete` removes key/value pairs from | ||
// a map. | ||
delete(m, "k2") | ||
fmt.Println("map:", m) | ||
|
||
// The optional second return value when getting a | ||
// value from a map indicates if the key was present | ||
// in the map. This can be used to disambiguate | ||
// between missing keys and keys with zero values | ||
// like `0` or `""`. Here we didn't need the value | ||
// itself, so we ignored it with the _blank identifier_ | ||
// `_`. | ||
_, prs := m["k2"] | ||
fmt.Println("prs:", prs) | ||
|
||
// You can also declare and initialize a new map in | ||
// the same line with this syntax. | ||
n := map[string]int{"foo": 1, "bar": 2} | ||
fmt.Println("map:", n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Code listing 17: https://play.golang.org/p/jYhjajBm7pQ | ||
|
||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
actor := map[string]int{ | ||
"Paltrow": 43, | ||
"Cruise": 53, | ||
"Redford": 79, | ||
"Diaz": 43, | ||
"Kilmer": 56, | ||
"Pacino": 75, | ||
"Ryder": 44, | ||
} | ||
|
||
for i := 1; i < 4; i++ { | ||
fmt.Printf("\nRUN NUMBER %d\n", i) | ||
for key, value := range actor { | ||
fmt.Printf("%s : %d years old\n", key, value) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Code listing 18: https://play.golang.org/p/GwFoxWxhuaF | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
) | ||
|
||
func main() { | ||
actor := map[string]int{ | ||
"Paltrow": 43, | ||
"Cruise": 53, | ||
"Redford": 79, | ||
"Diaz": 43, | ||
"Kilmer": 56, | ||
"Pacino": 75, | ||
"Ryder": 44, | ||
} | ||
|
||
// Store the keys in a slice | ||
var sortedActor []string | ||
for key := range actor { | ||
sortedActor = append(sortedActor, key) | ||
} | ||
// Sort the slice alphabetically | ||
sort.Strings(sortedActor) | ||
|
||
/* Retrieve the keys from the slice and use | ||
them to look up the map values */ | ||
for _, name := range sortedActor { | ||
fmt.Printf("%s : %d years old\n", name, actor[name]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Code listing 19: https://play.golang.org/p/ueJmA28U5EF | ||
|
||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
|
||
m := make(map[string]int) | ||
|
||
m["k1"] = 7 | ||
m["k2"] = 13 | ||
|
||
delete(m, "k2") | ||
|
||
if _, ok := m["k2"]; ok { | ||
fmt.Println("Ok") | ||
} else { | ||
fmt.Println("NotOk") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Code listing 2: https://play.golang.org/p/vyoCoYrzTpo | ||
|
||
package main | ||
|
||
import "fmt" | ||
|
||
func init() { | ||
fmt.Println("first") | ||
} | ||
|
||
func main() { | ||
fmt.Println("second") | ||
} |
Oops, something went wrong.