Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
iraniamir committed Jun 10, 2018
0 parents commit c67ab13
Show file tree
Hide file tree
Showing 38 changed files with 1,153 additions and 0 deletions.
Binary file added Go-Succinctly.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions README.md
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.

[![cover](https://github.com/Gommunity/gosuccinctly/blob/master/cover.png)](https://github.com/Gommunity/gosuccinctly)

## [*Download pdf*](https://github.com/Gommunity/gosuccinctly/blob/master/Go-Succinctly.pdf)
Binary file added cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions listing1.go
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")
}
20 changes: 20 additions & 0 deletions listing10.go
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)
}
78 changes: 78 additions & 0 deletions listing11.go
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)
}
20 changes: 20 additions & 0 deletions listing12.go
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)
}
14 changes: 14 additions & 0 deletions listing13.go
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)
}
24 changes: 24 additions & 0 deletions listing14.go
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))

}
24 changes: 24 additions & 0 deletions listing15.go
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))

}
52 changes: 52 additions & 0 deletions listing16.go
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)
}
24 changes: 24 additions & 0 deletions listing17.go
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)
}
}
}
34 changes: 34 additions & 0 deletions listing18.go
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])
}
}
21 changes: 21 additions & 0 deletions listing19.go
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")
}
}
13 changes: 13 additions & 0 deletions listing2.go
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")
}
Loading

0 comments on commit c67ab13

Please sign in to comment.