Skip to content

Commit c67ab13

Browse files
committed
v1.0.0
0 parents  commit c67ab13

38 files changed

+1153
-0
lines changed

Go-Succinctly.pdf

610 KB
Binary file not shown.

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Go Succinctly
2+
3+
This is the companion repo for [*Go Succinctly*](https://github.com/Gommunity/gosuccinctly) by Amir Irani.
4+
5+
[![cover](https://github.com/Gommunity/gosuccinctly/blob/master/cover.png)](https://github.com/Gommunity/gosuccinctly)
6+
7+
## [*Download pdf*](https://github.com/Gommunity/gosuccinctly/blob/master/Go-Succinctly.pdf)

cover.png

61.1 KB
Loading

listing1.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Code listing 1: https://play.golang.org/p/2C7wwJ6nxG
2+
3+
// Our first program will print the classic "hello world"
4+
// message. Here's the full source code.
5+
package main
6+
7+
import "fmt"
8+
9+
func main() {
10+
fmt.Println("hello world")
11+
}

listing10.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Code listing 10: https://play.golang.org/p/Lu01QP5AOri
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
)
8+
9+
func main() {
10+
myArray := [...]string{"Apples", "Oranges", "Bananas"}
11+
fmt.Printf("Initial array values: %v\n", myArray)
12+
myFunction(myArray)
13+
fmt.Printf("Final array values: %v\n", myArray)
14+
}
15+
16+
func myFunction(arr [3]string) {
17+
// Change Oranges to Strawberries
18+
arr[1] = "Strawberries"
19+
fmt.Printf("Array values in myFunction(): %v\n", arr)
20+
}

listing11.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Code listing 11: https://play.golang.org/p/Z3_U32sn8RF
2+
3+
// _Slices_ are a key data type in Go, giving a more
4+
// powerful interface to sequences than arrays.
5+
6+
package main
7+
8+
import "fmt"
9+
10+
func main() {
11+
12+
// Unlike arrays, slices are typed only by the
13+
// elements they contain (not the number of elements).
14+
// To create an empty slice with non-zero length, use
15+
// the builtin `make`. Here we make a slice of
16+
// `string`s of length `3` (initially zero-valued).
17+
s := make([]string, 3)
18+
fmt.Println("emp:", s)
19+
20+
// We can set and get just like with arrays.
21+
s[0] = "a"
22+
s[1] = "b"
23+
s[2] = "c"
24+
fmt.Println("set:", s)
25+
fmt.Println("get:", s[2])
26+
27+
// `len` returns the length of the slice as expected.
28+
fmt.Println("len:", len(s))
29+
30+
// In addition to these basic operations, slices
31+
// support several more that make them richer than
32+
// arrays. One is the builtin `append`, which
33+
// returns a slice containing one or more new values.
34+
// Note that we need to accept a return value from
35+
// `append` as we may get a new slice value.
36+
s = append(s, "d")
37+
s = append(s, "e", "f")
38+
fmt.Println("apd:", s)
39+
40+
// Slices can also be `copy`'d. Here we create an
41+
// empty slice `c` of the same length as `s` and copy
42+
// into `c` from `s`.
43+
c := make([]string, len(s))
44+
copy(c, s)
45+
fmt.Println("cpy:", c)
46+
47+
// Slices support a "slice" operator with the syntax
48+
// `slice[low:high]`. For example, this gets a slice
49+
// of the elements `s[2]`, `s[3]`, and `s[4]`.
50+
l := s[2:5]
51+
fmt.Println("sl1:", l)
52+
53+
// This slices up to (but excluding) `s[5]`.
54+
l = s[:5]
55+
fmt.Println("sl2:", l)
56+
57+
// And this slices up from (and including) `s[2]`.
58+
l = s[2:]
59+
fmt.Println("sl3:", l)
60+
61+
// We can declare and initialize a variable for slice
62+
// in a single line as well.
63+
t := []string{"g", "h", "i"}
64+
fmt.Println("dcl:", t)
65+
66+
// Slices can be composed into multi-dimensional data
67+
// structures. The length of the inner slices can
68+
// vary, unlike with multi-dimensional arrays.
69+
twoD := make([][]int, 3)
70+
for i := 0; i < 3; i++ {
71+
innerLen := i + 1
72+
twoD[i] = make([]int, innerLen)
73+
for j := 0; j < innerLen; j++ {
74+
twoD[i][j] = i + j
75+
}
76+
}
77+
fmt.Println("2d: ", twoD)
78+
}

listing12.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Code listing 12: https://play.golang.org/p/kHqSobJC2Yv
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
)
8+
9+
func main() {
10+
mySlice := []string{"Apples", "Oranges", "Bananas"}
11+
fmt.Printf("Initial slice values: %v\n", mySlice)
12+
myFunction(mySlice)
13+
fmt.Printf("Final slice values: %v\n", mySlice)
14+
}
15+
16+
func myFunction(fruits []string) {
17+
// Change Oranges to Strawberries
18+
fruits[1] = "Strawberries"
19+
fmt.Printf("Slice values in myFunction(): %v\n", fruits)
20+
}

listing13.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Code listing 13: https://play.golang.org/p/p3ewucCUFer
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
)
8+
9+
func main() {
10+
mySlice := make([]int, 4, 8)
11+
fmt.Printf("Initial Length: %d\n", len(mySlice))
12+
fmt.Printf("Capacity: %d\n", cap(mySlice))
13+
fmt.Printf("Contents: %v\n", mySlice)
14+
}

listing14.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Code listing: https://play.golang.org/p/BtiZKrqyImq
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
)
8+
9+
func main() {
10+
11+
mySlice := make([]int, 0, 8)
12+
mySlice = append(mySlice, 1, 3, 5, 7, 9, 11, 13, 17)
13+
14+
fmt.Printf("Contents: %v\n", mySlice)
15+
fmt.Printf("Number of Items: %d\n", len(mySlice))
16+
fmt.Printf("Capacity: %d\n", cap(mySlice))
17+
18+
mySlice[8] = 19
19+
20+
fmt.Printf("Contents: %v\n", mySlice)
21+
fmt.Printf("Number of Items: %d\n", len(mySlice))
22+
fmt.Printf("Capacity: %d\n", cap(mySlice))
23+
24+
}

listing15.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Code listing 15: https://play.golang.org/p/yE-1Rhccebv
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
)
8+
9+
func main() {
10+
11+
mySlice := make([]int, 0, 8)
12+
mySlice = append(mySlice, 1, 3, 5, 7, 9, 11, 13, 17)
13+
14+
fmt.Printf("Contents: %v\n", mySlice)
15+
fmt.Printf("Number of Items: %d\n", len(mySlice))
16+
fmt.Printf("Capacity: %d\n", cap(mySlice))
17+
18+
mySlice = append(mySlice, 19)
19+
20+
fmt.Printf("Contents: %v\n", mySlice)
21+
fmt.Printf("Number of Items: %d\n", len(mySlice))
22+
fmt.Printf("Capacity: %d\n", cap(mySlice))
23+
24+
}

listing16.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Code listing 16: https://play.golang.org/p/U67R66Oab8r
2+
3+
// _Maps_ are Go's built-in [associative data type](http://en.wikipedia.org/wiki/Associative_array)
4+
// (sometimes called _hashes_ or _dicts_ in other languages).
5+
6+
package main
7+
8+
import "fmt"
9+
10+
func main() {
11+
12+
// To create an empty map, use the builtin `make`:
13+
// `make(map[key-type]val-type)`.
14+
m := make(map[string]int)
15+
16+
// Set key/value pairs using typical `name[key] = val`
17+
// syntax.
18+
m["k1"] = 7
19+
m["k2"] = 13
20+
21+
// Printing a map with e.g. `fmt.Println` will show all of
22+
// its key/value pairs.
23+
fmt.Println("map:", m)
24+
25+
// Get a value for a key with `name[key]`.
26+
v1 := m["k1"]
27+
fmt.Println("v1: ", v1)
28+
29+
// The builtin `len` returns the number of key/value
30+
// pairs when called on a map.
31+
fmt.Println("len:", len(m))
32+
33+
// The builtin `delete` removes key/value pairs from
34+
// a map.
35+
delete(m, "k2")
36+
fmt.Println("map:", m)
37+
38+
// The optional second return value when getting a
39+
// value from a map indicates if the key was present
40+
// in the map. This can be used to disambiguate
41+
// between missing keys and keys with zero values
42+
// like `0` or `""`. Here we didn't need the value
43+
// itself, so we ignored it with the _blank identifier_
44+
// `_`.
45+
_, prs := m["k2"]
46+
fmt.Println("prs:", prs)
47+
48+
// You can also declare and initialize a new map in
49+
// the same line with this syntax.
50+
n := map[string]int{"foo": 1, "bar": 2}
51+
fmt.Println("map:", n)
52+
}

listing17.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Code listing 17: https://play.golang.org/p/jYhjajBm7pQ
2+
3+
package main
4+
5+
import "fmt"
6+
7+
func main() {
8+
actor := map[string]int{
9+
"Paltrow": 43,
10+
"Cruise": 53,
11+
"Redford": 79,
12+
"Diaz": 43,
13+
"Kilmer": 56,
14+
"Pacino": 75,
15+
"Ryder": 44,
16+
}
17+
18+
for i := 1; i < 4; i++ {
19+
fmt.Printf("\nRUN NUMBER %d\n", i)
20+
for key, value := range actor {
21+
fmt.Printf("%s : %d years old\n", key, value)
22+
}
23+
}
24+
}

listing18.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Code listing 18: https://play.golang.org/p/GwFoxWxhuaF
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"sort"
8+
)
9+
10+
func main() {
11+
actor := map[string]int{
12+
"Paltrow": 43,
13+
"Cruise": 53,
14+
"Redford": 79,
15+
"Diaz": 43,
16+
"Kilmer": 56,
17+
"Pacino": 75,
18+
"Ryder": 44,
19+
}
20+
21+
// Store the keys in a slice
22+
var sortedActor []string
23+
for key := range actor {
24+
sortedActor = append(sortedActor, key)
25+
}
26+
// Sort the slice alphabetically
27+
sort.Strings(sortedActor)
28+
29+
/* Retrieve the keys from the slice and use
30+
them to look up the map values */
31+
for _, name := range sortedActor {
32+
fmt.Printf("%s : %d years old\n", name, actor[name])
33+
}
34+
}

listing19.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Code listing 19: https://play.golang.org/p/ueJmA28U5EF
2+
3+
package main
4+
5+
import "fmt"
6+
7+
func main() {
8+
9+
m := make(map[string]int)
10+
11+
m["k1"] = 7
12+
m["k2"] = 13
13+
14+
delete(m, "k2")
15+
16+
if _, ok := m["k2"]; ok {
17+
fmt.Println("Ok")
18+
} else {
19+
fmt.Println("NotOk")
20+
}
21+
}

listing2.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Code listing 2: https://play.golang.org/p/vyoCoYrzTpo
2+
3+
package main
4+
5+
import "fmt"
6+
7+
func init() {
8+
fmt.Println("first")
9+
}
10+
11+
func main() {
12+
fmt.Println("second")
13+
}

0 commit comments

Comments
 (0)