From a95155e27d885db359ed7430ca18693ea9e07dd2 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Tue, 4 Feb 2020 21:44:36 -0600 Subject: [PATCH] initial commit --- append.go | 25 +++++++++++++++++++++++++ array.go | 14 ++++++++++++++ basic-types.go | 18 ++++++++++++++++++ constants.go | 14 ++++++++++++++ defer-multi.go | 13 +++++++++++++ defer.go | 9 +++++++++ exercise-fibonacci-closure.go | 25 +++++++++++++++++++++++++ exercise-loops-and-functions.go | 21 +++++++++++++++++++++ exercise-maps.go | 20 ++++++++++++++++++++ exercise-slices.go | 18 ++++++++++++++++++ exported-names.go | 10 ++++++++++ for-continued.go | 11 +++++++++++ for-is-gos-while.go | 11 +++++++++++ for.go | 11 +++++++++++ forever.go | 6 ++++++ function-closures.go | 21 +++++++++++++++++++++ function-values.go | 20 ++++++++++++++++++++ functions-continued.go | 11 +++++++++++ functions.go | 11 +++++++++++ hello.go | 7 +++++++ if-and-else.go | 23 +++++++++++++++++++++++ if-with-a-short-statement.go | 20 ++++++++++++++++++++ if.go | 17 +++++++++++++++++ imports.go | 10 ++++++++++ making-slices.go | 22 ++++++++++++++++++++++ map-literals-continued.go | 16 ++++++++++++++++ map-literals.go | 20 ++++++++++++++++++++ maps.go | 17 +++++++++++++++++ multiple-results.go | 12 ++++++++++++ mutating-maps.go | 19 +++++++++++++++++++ named-results.go | 13 +++++++++++++ nil-slices.go | 11 +++++++++++ numeric-constants.go | 19 +++++++++++++++++++ packages.go | 14 ++++++++++++++ pointers.go | 16 ++++++++++++++++ range-continued.go | 13 +++++++++++++ range.go | 11 +++++++++++ sandbox.go | 12 ++++++++++++ short-variable-declaration.go | 10 ++++++++++ slice-bounds.go | 16 ++++++++++++++++ slice-len-cap.go | 24 ++++++++++++++++++++++++ slice-literals.go | 24 ++++++++++++++++++++++++ slices-of-slice.go | 26 ++++++++++++++++++++++++++ slices-pointers.go | 20 ++++++++++++++++++++ slices.go | 10 ++++++++++ struct-fields.go | 14 ++++++++++++++ struct-literal.go | 18 ++++++++++++++++++ struct-pointers.go | 15 +++++++++++++++ structs.go | 12 ++++++++++++ switch-evaluation-order.go | 21 +++++++++++++++++++++ switch-with-no-condition.go | 18 ++++++++++++++++++ switch.go | 20 ++++++++++++++++++++ type-conversions.go | 13 +++++++++++++ type-inference.go | 8 ++++++++ variables-with-initializers.go | 10 ++++++++++ variables.go | 10 ++++++++++ zero.go | 12 ++++++++++++ 57 files changed, 882 insertions(+) create mode 100644 append.go create mode 100644 array.go create mode 100644 basic-types.go create mode 100644 constants.go create mode 100644 defer-multi.go create mode 100644 defer.go create mode 100644 exercise-fibonacci-closure.go create mode 100644 exercise-loops-and-functions.go create mode 100644 exercise-maps.go create mode 100644 exercise-slices.go create mode 100644 exported-names.go create mode 100644 for-continued.go create mode 100644 for-is-gos-while.go create mode 100644 for.go create mode 100644 forever.go create mode 100644 function-closures.go create mode 100644 function-values.go create mode 100644 functions-continued.go create mode 100644 functions.go create mode 100644 hello.go create mode 100644 if-and-else.go create mode 100644 if-with-a-short-statement.go create mode 100644 if.go create mode 100644 imports.go create mode 100644 making-slices.go create mode 100644 map-literals-continued.go create mode 100644 map-literals.go create mode 100644 maps.go create mode 100644 multiple-results.go create mode 100644 mutating-maps.go create mode 100644 named-results.go create mode 100644 nil-slices.go create mode 100644 numeric-constants.go create mode 100644 packages.go create mode 100644 pointers.go create mode 100644 range-continued.go create mode 100644 range.go create mode 100644 sandbox.go create mode 100644 short-variable-declaration.go create mode 100644 slice-bounds.go create mode 100644 slice-len-cap.go create mode 100644 slice-literals.go create mode 100644 slices-of-slice.go create mode 100644 slices-pointers.go create mode 100644 slices.go create mode 100644 struct-fields.go create mode 100644 struct-literal.go create mode 100644 struct-pointers.go create mode 100644 structs.go create mode 100644 switch-evaluation-order.go create mode 100644 switch-with-no-condition.go create mode 100644 switch.go create mode 100644 type-conversions.go create mode 100644 type-inference.go create mode 100644 variables-with-initializers.go create mode 100644 variables.go create mode 100644 zero.go diff --git a/append.go b/append.go new file mode 100644 index 0000000..209e07f --- /dev/null +++ b/append.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +func main() { + var s []int + printSlice(s) + + // append works on nil slices + s = append(s, 0) + printSlice(s) + + // the slice grows as needed + s = append(s, 1) + printSlice(s) + + // we can add more than one element at a time + s = append(s, 2, 3, 4) + printSlice(s) + +} + +func printSlice(s []int) { + fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s) +} diff --git a/array.go b/array.go new file mode 100644 index 0000000..910e778 --- /dev/null +++ b/array.go @@ -0,0 +1,14 @@ +package main + +import "fmt" + +func main() { + var a[2] string + a[0] = "Hello" + a[1] = "World" + fmt.Println(a[0], a[1]) + fmt.Println(a) + + primes := [6]int{2, 3, 5, 7, 11, 13} + fmt.Println(primes) +} diff --git a/basic-types.go b/basic-types.go new file mode 100644 index 0000000..a92b909 --- /dev/null +++ b/basic-types.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "math/cmplx" +) + +var ( + ToBe bool = false + MaxInt uint64 = 1 << 64 - 1 + z complex128 = cmplx.Sqrt(-5 + 12i) +) + +func main() { + fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe) + fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt) + fmt.Printf("Type: %T Value: %v\n", z, z) +} diff --git a/constants.go b/constants.go new file mode 100644 index 0000000..aaf50b6 --- /dev/null +++ b/constants.go @@ -0,0 +1,14 @@ +package main + +import "fmt" + +const Pi = 3.14 + +func main() { + const World = "world" + fmt.Println("Hello", World) + fmt.Println("Hello", Pi, "Day") + + const Truth = true + fmt.Println("Go rules?", Truth) +} diff --git a/defer-multi.go b/defer-multi.go new file mode 100644 index 0000000..d8ec044 --- /dev/null +++ b/defer-multi.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func main () { + fmt.Println("counting") + + for i := 0; i < 10; i++ { + defer fmt.Println(i) + } + + fmt.Println("done") +} diff --git a/defer.go b/defer.go new file mode 100644 index 0000000..c52615e --- /dev/null +++ b/defer.go @@ -0,0 +1,9 @@ +package main + +import "fmt" + +func main() { + defer fmt.Println("world") + + fmt.Println("hello") +} diff --git a/exercise-fibonacci-closure.go b/exercise-fibonacci-closure.go new file mode 100644 index 0000000..130997a --- /dev/null +++ b/exercise-fibonacci-closure.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +// fibonacci is a function that returns +// a function that returns an int. +func fibonacci() func() int { + x := 0 + y := 1 + z := 0 + return func() int { + z, x, y = x, y, x + y + if z == 0 { + return 0 + } + return z + } +} + +func main() { + f := fibonacci() + for i := 0; i < 10; i++ { + fmt.Println(f()) + } +} diff --git a/exercise-loops-and-functions.go b/exercise-loops-and-functions.go new file mode 100644 index 0000000..6b8995a --- /dev/null +++ b/exercise-loops-and-functions.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "math" +) + +func Sqrt(x float64) float64 { + var p float64 + for z := 1.0; z != p; z -= (z * z - x) / (2 * z) { + p = z + } + return p +} + +func main() { + fmt.Printf("Stdlib: %g, Mine: %g\n", math.Sqrt(333), Sqrt(333)) + fmt.Printf("Stdlib: %g, Mine: %g\n", math.Sqrt(33), Sqrt(33)) + fmt.Printf("Stdlib: %g, Mine: %g\n", math.Sqrt(17), Sqrt(17)) + fmt.Printf("Stdlib: %g, Mine: %g\n", math.Sqrt(4), Sqrt(4)) +} diff --git a/exercise-maps.go b/exercise-maps.go new file mode 100644 index 0000000..8dfe1c1 --- /dev/null +++ b/exercise-maps.go @@ -0,0 +1,20 @@ +package main + +import ( + "strings" + "golang.org/x/tour/wc" +) + +var count map[string]int + +func WordCount(s string) map[string]int { + count = make(map[string]int) + for _, w := range strings.Fields(s) { + count[w]++ + } + return count +} + +func main() { + wc.Test(WordCount) +} diff --git a/exercise-slices.go b/exercise-slices.go new file mode 100644 index 0000000..fe87814 --- /dev/null +++ b/exercise-slices.go @@ -0,0 +1,18 @@ +package main + +import "golang.org/x/tour/pic" + +func Pic(dx, dy int) [][]uint8 { + pic := make([][]uint8, dy) + for y := range pic { + pic[y] = make([]uint8, dx) + for x := range pic[y] { + pic[y][x] = uint8(x) ^ uint8(y) + } + } + return pic +} + +func main() { + pic.Show(Pic) +} diff --git a/exported-names.go b/exported-names.go new file mode 100644 index 0000000..3b9ee32 --- /dev/null +++ b/exported-names.go @@ -0,0 +1,10 @@ +package main + +import ( + "fmt" + "math" +) + +func main() { + fmt.Println(math.Pi) +} diff --git a/for-continued.go b/for-continued.go new file mode 100644 index 0000000..6f1c33b --- /dev/null +++ b/for-continued.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func main() { + sum := 1 + for sum < 1000 { + sum += sum + } + fmt.Println(sum) +} diff --git a/for-is-gos-while.go b/for-is-gos-while.go new file mode 100644 index 0000000..6f1c33b --- /dev/null +++ b/for-is-gos-while.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func main() { + sum := 1 + for sum < 1000 { + sum += sum + } + fmt.Println(sum) +} diff --git a/for.go b/for.go new file mode 100644 index 0000000..e495cd3 --- /dev/null +++ b/for.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func main() { + sum := 0 + for i := 0; i < 10; i++ { + sum += i + } + fmt.Println(sum) +} diff --git a/forever.go b/forever.go new file mode 100644 index 0000000..06c48fd --- /dev/null +++ b/forever.go @@ -0,0 +1,6 @@ +package main + +func main() { + for { + } +} diff --git a/function-closures.go b/function-closures.go new file mode 100644 index 0000000..2fd98c5 --- /dev/null +++ b/function-closures.go @@ -0,0 +1,21 @@ +package main + +import "fmt" + +func adder() func(int) int { + sum := 0 + return func(x int) int { + sum += x + return sum + } +} + +func main() { + pos, neg := adder(), adder() + for i := 0; i < 10; i++ { + fmt.Println( + pos(i), + neg(-2*i), + ) + } +} diff --git a/function-values.go b/function-values.go new file mode 100644 index 0000000..d8d5831 --- /dev/null +++ b/function-values.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "math" +) + +func compute(fn func(float64, float64) float64) float64 { + return fn(3, 4) +} + +func main() { + hypot := func(x, y float64) float64 { + return math.Sqrt(x*x + y*y) + } + fmt.Println(hypot(5, 12)) + + fmt.Println(compute(hypot)) + fmt.Println(compute(math.Pow)) +} diff --git a/functions-continued.go b/functions-continued.go new file mode 100644 index 0000000..28eba37 --- /dev/null +++ b/functions-continued.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func add(x, y int) int { + return x + y +} + +func main() { + fmt.Println(add(42, 13)) +} diff --git a/functions.go b/functions.go new file mode 100644 index 0000000..c2cbaac --- /dev/null +++ b/functions.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func add(x int, y int) int { + return x + y +} + +func main() { + fmt.Println(add(42, 13)) +} diff --git a/hello.go b/hello.go new file mode 100644 index 0000000..ef25884 --- /dev/null +++ b/hello.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello, world!") +} diff --git a/if-and-else.go b/if-and-else.go new file mode 100644 index 0000000..8113ad7 --- /dev/null +++ b/if-and-else.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "math" +) + +func pow(x, n, lim float64) float64 { + if v := math.Pow(x, n); v < lim { + return v + } else { + fmt.Printf("%g >= %g\n", v, lim) + } + // can't use v here, though + return lim +} + +func main() { + fmt.Println( + pow(3, 2, 10), + pow(3, 3, 20), + ) +} diff --git a/if-with-a-short-statement.go b/if-with-a-short-statement.go new file mode 100644 index 0000000..c613a0b --- /dev/null +++ b/if-with-a-short-statement.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "math" +) + +func pow(x, n, lim float64) float64 { + if v := math.Pow(x, n); v < lim { + return v + } + return lim +} + +func main() { + fmt.Println( + pow(3, 2, 10), + pow(3, 3, 20), + ) +} diff --git a/if.go b/if.go new file mode 100644 index 0000000..5554492 --- /dev/null +++ b/if.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "math" +) + +func sqrt(x float64) string { + if x < 0 { + return sqrt(-x) + "i" + } + return fmt.Sprint(math.Sqrt(x)) +} + +func main() { + fmt.Println(sqrt(2), sqrt(-4)) +} diff --git a/imports.go b/imports.go new file mode 100644 index 0000000..04372de --- /dev/null +++ b/imports.go @@ -0,0 +1,10 @@ +package main + +import ( + "fmt" + "math" +) + +func main() { + fmt.Printf("Now you have %g problems.\n", math.Sqrt(7)) +} diff --git a/making-slices.go b/making-slices.go new file mode 100644 index 0000000..fcc1c23 --- /dev/null +++ b/making-slices.go @@ -0,0 +1,22 @@ +package main + +import "fmt" + +func main() { + a := make([]int, 5) + printSlice("a", a) + + b := make([]int, 0, 5) + printSlice("b", b) + + c := b[:2] + printSlice("c", c) + + d := c[2:5] + printSlice("d", d) +} + +func printSlice(s string, x []int) { + fmt.Printf("%s len=%d cap=%d %v\n", + s, len(x), cap(x), x) +} diff --git a/map-literals-continued.go b/map-literals-continued.go new file mode 100644 index 0000000..755de8d --- /dev/null +++ b/map-literals-continued.go @@ -0,0 +1,16 @@ +package main + +import "fmt" + +type Vertex struct { + Lat, Long float64 +} + +var m = map[string]Vertex { + "Bell Labs": {40.68433, -74.39967}, + "Google": {37.42202, -122.08408}, +} + +func main() { + fmt.Println(m) +} diff --git a/map-literals.go b/map-literals.go new file mode 100644 index 0000000..317db95 --- /dev/null +++ b/map-literals.go @@ -0,0 +1,20 @@ +package main + +import "fmt" + +type Vertex struct { + Lat, Long float64 +} + +var m = map[string]Vertex { + "Bell Labs": Vertex{ + 40.68433, -74.39967, + }, + "Google": Vertex{ + 37.42202, -122.08408, + }, +} + +func main() { + fmt.Println(m) +} diff --git a/maps.go b/maps.go new file mode 100644 index 0000000..279839e --- /dev/null +++ b/maps.go @@ -0,0 +1,17 @@ +package main + +import "fmt" + +type Vertex struct { + Lat, Long float64 +} + +var m map[string]Vertex + +func main() { + m = make(map[string]Vertex) + m["Bell Labs"] = Vertex{ + 40.68433, -74.39967, + } + fmt.Println(m["Bell Labs"]) +} diff --git a/multiple-results.go b/multiple-results.go new file mode 100644 index 0000000..44bdfc9 --- /dev/null +++ b/multiple-results.go @@ -0,0 +1,12 @@ +package main + +import "fmt" + +func swap(x, y string) (string, string) { + return y, x +} + +func main() { + a, b := swap("hello", "world") + fmt.Println(a, b) +} diff --git a/mutating-maps.go b/mutating-maps.go new file mode 100644 index 0000000..29e4087 --- /dev/null +++ b/mutating-maps.go @@ -0,0 +1,19 @@ +package main + +import "fmt" + +func main() { + m := make(map[string]int) + + m["Answer"] = 42 + fmt.Println("The value:", m["Answer"]) + + m["Answer"] = 48 + fmt.Println("The value:", m["Answer"]) + + delete(m, "Answer") + fmt.Println("The value:", m["Answer"]) + + v, ok := m["Answer"] + fmt.Println("The value:", v, "Present?", ok) +} diff --git a/named-results.go b/named-results.go new file mode 100644 index 0000000..b805215 --- /dev/null +++ b/named-results.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func split(sum int) (x, y int) { + x = sum * 4 / 9 + y = sum - x + return +} + +func main() { + fmt.Println(split(17)) +} diff --git a/nil-slices.go b/nil-slices.go new file mode 100644 index 0000000..ea75106 --- /dev/null +++ b/nil-slices.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func main() { + var s []int + fmt.Println(s, len(s), cap(s)) + if s == nil { + fmt.Println("nil!") + } +} diff --git a/numeric-constants.go b/numeric-constants.go new file mode 100644 index 0000000..1177ab7 --- /dev/null +++ b/numeric-constants.go @@ -0,0 +1,19 @@ +package main + +import "fmt" + +const ( + Big = 1 << 100 + Small = Big >> 99 +) + +func needInt(x int) int { return x*10 + 1 } +func needFloat(x float64) float64 { + return x * 0.1 +} + +func main() { + fmt.Println(needInt(Small)) + fmt.Println(needFloat(Small)) + fmt.Println(needFloat(Big)) +} diff --git a/packages.go b/packages.go new file mode 100644 index 0000000..9ce5c5d --- /dev/null +++ b/packages.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + "math/rand" + "time" +) + +func main() { + seed := time.Now().UnixNano() + rand.Seed(seed) + fmt.Println("Seed: ", seed) + fmt.Println("My favorite number is", rand.Intn(10)) +} diff --git a/pointers.go b/pointers.go new file mode 100644 index 0000000..ecd9ca2 --- /dev/null +++ b/pointers.go @@ -0,0 +1,16 @@ +package main + +import "fmt" + +func main() { + i, j := 42, 2701 + + p := &i + fmt.Println(*p) + *p = 21 + p = &j + fmt.Println(i) + + *p = *p / 37 + fmt.Println(j) +} diff --git a/range-continued.go b/range-continued.go new file mode 100644 index 0000000..c11a18a --- /dev/null +++ b/range-continued.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func main() { + pow := make([]int, 10) + for i := range pow { + pow[i] = 1 << uint(i) // == 2**i + } + for _, value := range pow { + fmt.Printf("%d\n", value) + } +} diff --git a/range.go b/range.go new file mode 100644 index 0000000..c74255d --- /dev/null +++ b/range.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +var pow = []int{1, 2, 3, 4, 8, 16, 32, 64, 128} + +func main() { + for i, v := range pow { + fmt.Printf("2**%d = %d\n", i, v) + } +} diff --git a/sandbox.go b/sandbox.go new file mode 100644 index 0000000..711618a --- /dev/null +++ b/sandbox.go @@ -0,0 +1,12 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + fmt.Println("Welcome to the playground!") + + fmt.Println("The time is", time.Now()) +} diff --git a/short-variable-declaration.go b/short-variable-declaration.go new file mode 100644 index 0000000..e6f70b8 --- /dev/null +++ b/short-variable-declaration.go @@ -0,0 +1,10 @@ +package main + +import "fmt" + +func main() { + var i, j int = 1, 2 + k := 3 + c, python, java := true, false, "no!" + fmt.Println(i, j, k, c, python, java) +} diff --git a/slice-bounds.go b/slice-bounds.go new file mode 100644 index 0000000..e91cc46 --- /dev/null +++ b/slice-bounds.go @@ -0,0 +1,16 @@ +package main + +import "fmt" + +func main() { + s := []int{2, 3, 5, 7, 11, 13} + + s = s[1:4] + fmt.Println(s) + + s = s[:2] + fmt.Println(s) + + s = s[1:] + fmt.Println(s) +} diff --git a/slice-len-cap.go b/slice-len-cap.go new file mode 100644 index 0000000..a09abd4 --- /dev/null +++ b/slice-len-cap.go @@ -0,0 +1,24 @@ +package main + +import "fmt" + +func main() { + s := []int{2, 3, 5, 7, 11, 13} + printSlice(s) + + // Slice the slive to give it zero length. + s = s[:0] + printSlice(s) + + // Extend its length + s = s[:4] + printSlice(s) + + // Drop its first two values + s = s[2:] + printSlice(s) +} + +func printSlice(s []int) { + fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s) +} diff --git a/slice-literals.go b/slice-literals.go new file mode 100644 index 0000000..1e63cd3 --- /dev/null +++ b/slice-literals.go @@ -0,0 +1,24 @@ +package main + +import "fmt" + +func main() { + q := []int{2, 3, 5, 7, 11, 13} + fmt.Println(q) + + r := []bool{true, false, true, true, false, true} + fmt.Println(r) + + s := []struct { + i int + b bool + } { + {2, true}, + {3, false}, + {5, true}, + {7, true}, + {11, false}, + {13, true}, + } + fmt.Println(s) +} diff --git a/slices-of-slice.go b/slices-of-slice.go new file mode 100644 index 0000000..6977a16 --- /dev/null +++ b/slices-of-slice.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "strings" +) + +func main() { + // Create a tic-tac-toe board + board := [][]string{ + []string{"_", "_", "_"}, + []string{"_", "_", "_"}, + []string{"_", "_", "_"}, + } + + // The players take turns + board[0][0] = "X" + board[2][2] = "O" + board[1][2] = "X" + board[1][0] = "O" + board[0][2] = "X" + + for i := 0; i < len(board); i++ { + fmt.Printf("%s\n", strings.Join(board[i], " ")) + } +} diff --git a/slices-pointers.go b/slices-pointers.go new file mode 100644 index 0000000..a261fc0 --- /dev/null +++ b/slices-pointers.go @@ -0,0 +1,20 @@ +package main + +import "fmt" + +func main() { + names := [4]string{ + "John", + "Paul", + "George", + "Ringo", + } + fmt.Println(names) + + a := names[0:2] + b := names[1:3] + + b[0] = "XXX" + fmt.Println(a, b) + fmt.Println(names) +} diff --git a/slices.go b/slices.go new file mode 100644 index 0000000..4970c46 --- /dev/null +++ b/slices.go @@ -0,0 +1,10 @@ +package main + +import "fmt" + +func main() { + primes := [6]int{2, 3, 5, 7, 11, 13} + + var s []int = primes[1:4] + fmt.Println(s) +} diff --git a/struct-fields.go b/struct-fields.go new file mode 100644 index 0000000..d205016 --- /dev/null +++ b/struct-fields.go @@ -0,0 +1,14 @@ +package main + +import "fmt" + +type Vertex struct { + X int + Y int +} + +func main() { + v := Vertex{1, 2} + v.X = 4 + fmt.Println(v.X) +} diff --git a/struct-literal.go b/struct-literal.go new file mode 100644 index 0000000..3ee388b --- /dev/null +++ b/struct-literal.go @@ -0,0 +1,18 @@ +package main + +import "fmt" + +type Vertex struct { + X, Y int +} + +var ( + v1 = Vertex{1, 2} // has type Vertex + v2 = Vertex{X: 1} // Y:0 is implicit + v3 = Vertex{} // X:0 and Y:0 + p = &Vertex{1, 2} // has type *Vertex +) + +func main() { + fmt.Println(v1, p, v2, v3) +} diff --git a/struct-pointers.go b/struct-pointers.go new file mode 100644 index 0000000..7b33aa9 --- /dev/null +++ b/struct-pointers.go @@ -0,0 +1,15 @@ +package main + +import "fmt" + +type Vertex struct { + X int + Y int +} + +func main() { + v := Vertex{1, 2} + p := &v + p.X = 1e9 + fmt.Println(v) +} diff --git a/structs.go b/structs.go new file mode 100644 index 0000000..4d825ce --- /dev/null +++ b/structs.go @@ -0,0 +1,12 @@ +package main + +import "fmt" + +type Vertex struct { + X int + Y int +} + +func main() { + fmt.Println(Vertex{1, 2}) +} diff --git a/switch-evaluation-order.go b/switch-evaluation-order.go new file mode 100644 index 0000000..fb43257 --- /dev/null +++ b/switch-evaluation-order.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + fmt.Println("When's Saturday?") + today := time.Now().Weekday() + switch time.Saturday { + case today + 0: + fmt.Println("Today.") + case today + 1: + fmt.Println("Tomorrow.") + case today + 2: + fmt.Println("In two days.") + default: + fmt.Println("Too far away.") + } +} diff --git a/switch-with-no-condition.go b/switch-with-no-condition.go new file mode 100644 index 0000000..e192e85 --- /dev/null +++ b/switch-with-no-condition.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + t := time.Now() + switch { + case t.Hour() < 12: + fmt.Println("Good morning!") + case t.Hour() < 17: + fmt.Println("Good afternoon.") + default: + fmt.Println("Good evening.") + } +} diff --git a/switch.go b/switch.go new file mode 100644 index 0000000..c49b6d0 --- /dev/null +++ b/switch.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "runtime" +) + +func main() { + fmt.Print("Go runs on ") + switch os := runtime.GOOS; os { + case "darwin": + fmt.Println("OS X.") + case "linux": + fmt.Println("Linux.") + default: + // freebsd, openbsd + // plan9, windows ... + fmt.Print("%s.\n", os) + } +} diff --git a/type-conversions.go b/type-conversions.go new file mode 100644 index 0000000..39d7b79 --- /dev/null +++ b/type-conversions.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + "math" +) + +func main() { + var x, y int = 3, 4 + var f float64 = math.Sqrt(float64(x*x + y*y)) + var z uint = uint(f) + fmt.Println(x, y, z) +} diff --git a/type-inference.go b/type-inference.go new file mode 100644 index 0000000..f8b7a7e --- /dev/null +++ b/type-inference.go @@ -0,0 +1,8 @@ +package main + +import "fmt" + +func main() { + v := 42 + fmt.Printf("v is of type %T\n", v) +} diff --git a/variables-with-initializers.go b/variables-with-initializers.go new file mode 100644 index 0000000..c940791 --- /dev/null +++ b/variables-with-initializers.go @@ -0,0 +1,10 @@ +package main + +import "fmt" + +var i, j int = 1, 2 + +func main() { + var c, python, java = true, false, "no!" + fmt.Println(i, j, c, python, java) +} diff --git a/variables.go b/variables.go new file mode 100644 index 0000000..f02747f --- /dev/null +++ b/variables.go @@ -0,0 +1,10 @@ +package main + +import "fmt" + +var c, python, java bool + +func main() { + var i int + fmt.Println(i, c, python, java) +} diff --git a/zero.go b/zero.go new file mode 100644 index 0000000..0a73485 --- /dev/null +++ b/zero.go @@ -0,0 +1,12 @@ +package main + +import "fmt" + +func main() { + var i int + var f float64 + var b bool + var s string + + fmt.Printf("%v %v %v %q\n", i, f, b, s) +}