From afe165089d508627ff028e5a6a6004e93833d543 Mon Sep 17 00:00:00 2001 From: Robert Jeutter Date: Fri, 31 Dec 2021 15:36:34 +0100 Subject: [PATCH] starting go by example --- 5-go-by-example/1-hello-world.go | 7 ++++ 5-go-by-example/10-maps.go | 36 +++++++++++++++++++ 5-go-by-example/2-values.go | 19 ++++++++++ 5-go-by-example/3-variables.go | 25 +++++++++++++ 5-go-by-example/4-constants.go | 26 ++++++++++++++ 5-go-by-example/5-for.go | 34 ++++++++++++++++++ 5-go-by-example/6-if-else.go | 27 ++++++++++++++ 5-go-by-example/7-switch.go | 55 +++++++++++++++++++++++++++++ 5-go-by-example/8-arrays.go | 33 ++++++++++++++++++ 5-go-by-example/9-slices.go | 60 ++++++++++++++++++++++++++++++++ 5-go-by-example/readme.md | 18 ++++++++++ 11 files changed, 340 insertions(+) create mode 100644 5-go-by-example/1-hello-world.go create mode 100644 5-go-by-example/10-maps.go create mode 100644 5-go-by-example/2-values.go create mode 100644 5-go-by-example/3-variables.go create mode 100644 5-go-by-example/4-constants.go create mode 100644 5-go-by-example/5-for.go create mode 100644 5-go-by-example/6-if-else.go create mode 100644 5-go-by-example/7-switch.go create mode 100644 5-go-by-example/8-arrays.go create mode 100644 5-go-by-example/9-slices.go create mode 100644 5-go-by-example/readme.md diff --git a/5-go-by-example/1-hello-world.go b/5-go-by-example/1-hello-world.go new file mode 100644 index 0000000..c048119 --- /dev/null +++ b/5-go-by-example/1-hello-world.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("hello world") +} diff --git a/5-go-by-example/10-maps.go b/5-go-by-example/10-maps.go new file mode 100644 index 0000000..33c7ad9 --- /dev/null +++ b/5-go-by-example/10-maps.go @@ -0,0 +1,36 @@ +package main + +import "fmt" + +// Maps are Go’s built-in associative data type +func main() { + + // create an empty map, use the builtin: 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 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) + + // len returns the number of key/value pairs + fmt.Println("len:", len(m)) + + // delete removes key/value pairs + delete(m, "k2") + fmt.Println("map:", m) + + // optional second return value when getting a value from a map indicates if the key was present in the map + _, prs := m["k2"] + fmt.Println("prs:", prs) + + // declare and initialize a new map in the same line + n := map[string]int{"foo": 1, "bar": 2} + fmt.Println("map:", n) +} diff --git a/5-go-by-example/2-values.go b/5-go-by-example/2-values.go new file mode 100644 index 0000000..a7fe10c --- /dev/null +++ b/5-go-by-example/2-values.go @@ -0,0 +1,19 @@ +package main + +import "fmt" + +func main() { + // Strings + fmt.Println("go" + "lang") + + // Integers + fmt.Println("1+1 =", 1+1) + + // Floats + fmt.Println("7.0/3.0 =", 7.0/3.0) + + //Boolean with operators + fmt.Println(true && false) + fmt.Println(true || false) + fmt.Println(!true) +} diff --git a/5-go-by-example/3-variables.go b/5-go-by-example/3-variables.go new file mode 100644 index 0000000..2adac80 --- /dev/null +++ b/5-go-by-example/3-variables.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +func main() { + // var declares 1 or more variables + var a = "initial" + fmt.Println(a) + + // here are multiple variables declared + var b, c int = 1, 2 + fmt.Println(b, c) + + // go infers the type of initialized variables + var d = true + fmt.Println(d) + + // Variables declared without a corresponding initialization are zero-valued + var e int + fmt.Println(e) + + // shorthand for declaring and initializing a variable + f := "apple" + fmt.Println(f) +} diff --git a/5-go-by-example/4-constants.go b/5-go-by-example/4-constants.go new file mode 100644 index 0000000..4b993cf --- /dev/null +++ b/5-go-by-example/4-constants.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "math" +) + +// const declares a constant value. +const s string = "constant" + +func main() { + fmt.Println(s) + + // can appear anywhere a var statement can + const n = 500000000 + + // expressions perform arithmetic with arbitrary precision + const d = 3e20 / n + fmt.Println(d) + + // numeric constants have no type until it’s given one + fmt.Println(int64(d)) + + // numbers can be given a type by using it in a context that requires one + fmt.Println(math.Sin(n)) +} diff --git a/5-go-by-example/5-for.go b/5-go-by-example/5-for.go new file mode 100644 index 0000000..8fd24d6 --- /dev/null +++ b/5-go-by-example/5-for.go @@ -0,0 +1,34 @@ +package main + +import "fmt" + +// for is Go’s only looping construct + +func main() { + + // basic type with a single condition + i := 1 + for i <= 3 { + fmt.Println(i) + i = i + 1 + } + + // classic initial/condition/after + for j := 7; j <= 9; j++ { + fmt.Println(j) + } + + // without a condition will loop repeatedly until break or return + for { + fmt.Println("loop") + break + } + + // continue to the next iteration + for n := 0; n <= 5; n++ { + if n%2 == 0 { + continue + } + fmt.Println(n) + } +} diff --git a/5-go-by-example/6-if-else.go b/5-go-by-example/6-if-else.go new file mode 100644 index 0000000..e9406c4 --- /dev/null +++ b/5-go-by-example/6-if-else.go @@ -0,0 +1,27 @@ +package main + +import "fmt" + +func main() { + + // basic + if 7%2 == 0 { + fmt.Println("7 is even") + } else { + fmt.Println("7 is odd") + } + + // if statement without an else + if 8%4 == 0 { + fmt.Println("8 is divisible by 4") + } + + // statements can precede conditionals; any variables declared in this statement are available in all branches + if num := 9; num < 0 { + fmt.Println(num, "is negative") + } else if num < 10 { + fmt.Println(num, "has 1 digit") + } else { + fmt.Println(num, "has multiple digits") + } +} diff --git a/5-go-by-example/7-switch.go b/5-go-by-example/7-switch.go new file mode 100644 index 0000000..00b3adb --- /dev/null +++ b/5-go-by-example/7-switch.go @@ -0,0 +1,55 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + + // basic + i := 2 + fmt.Print("Write ", i, " as ") + switch i { + case 1: + fmt.Println("one") + case 2: + fmt.Println("two") + case 3: + fmt.Println("three") + } + + // use commas to separate multiple expressions in the same case statement + // also default case is used + switch time.Now().Weekday() { + case time.Saturday, time.Sunday: + fmt.Println("It's the weekend") + default: + fmt.Println("It's a weekday") + } + + // switch without an expression is an alternate way to express if/else logic + t := time.Now() + switch { + case t.Hour() < 12: + fmt.Println("It's before noon") + default: + fmt.Println("It's after noon") + } + + // type switch compares types instead of values + // here it discovers the type of an interface value + whatAmI := func(i interface{}) { + switch t := i.(type) { + case bool: + fmt.Println("I'm a bool") + case int: + fmt.Println("I'm an int") + default: + fmt.Printf("Don't know type %T\n", t) + } + } + whatAmI(true) + whatAmI(1) + whatAmI("hey") +} diff --git a/5-go-by-example/8-arrays.go b/5-go-by-example/8-arrays.go new file mode 100644 index 0000000..a2acebf --- /dev/null +++ b/5-go-by-example/8-arrays.go @@ -0,0 +1,33 @@ +package main + +import "fmt" + +// an array is a numbered sequence of elements of a specific length +func main() { + + // array a that will hold exactly 5 ints + var a [5]int + fmt.Println("emp:", a) + + // set/get a value at an index using the array[index]=value syntax + a[4] = 100 + fmt.Println("set:", a) + fmt.Println("get:", a[4]) + + // builtin len returns the length of an array + fmt.Println("len:", len(a)) + + // declare and initialize an array in one line + b := [5]int{1, 2, 3, 4, 5} + fmt.Println("dcl:", b) + + // Array types are one-dimensional + // compose types to build multi-dimensional data structures + var twoD [2][3]int + for i := 0; i < 2; i++ { + for j := 0; j < 3; j++ { + twoD[i][j] = i + j + } + } + fmt.Println("2d: ", twoD) +} diff --git a/5-go-by-example/9-slices.go b/5-go-by-example/9-slices.go new file mode 100644 index 0000000..b9f004b --- /dev/null +++ b/5-go-by-example/9-slices.go @@ -0,0 +1,60 @@ +package main + +import "fmt" + +// Slices are a key data type in Go, giving a more powerful interface to sequences than arrays +func main() { + + // slices are typed only by the elements they contain (not the number of elements) + // Here make a slice of strings of length 3 (initially zero-valued) + s := make([]string, 3) + fmt.Println("emp:", s) + + // set and get 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)) + + // returns a slice containing one or more new values + s = append(s, "d") + s = append(s, "e", "f") + fmt.Println("apd:", s) + + // 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 operators with the syntax slice[low:high] + l := s[2:5] + fmt.Println("sl1:", l) + + // slices up to (but excluding) s[5] + l = s[:5] + fmt.Println("sl2:", l) + + // slices up from (and including) s[2] + l = s[2:] + fmt.Println("sl3:", l) + + // declare and initialize a variable for slice in a single line + 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) +} diff --git a/5-go-by-example/readme.md b/5-go-by-example/readme.md new file mode 100644 index 0000000..2cfd09b --- /dev/null +++ b/5-go-by-example/readme.md @@ -0,0 +1,18 @@ +# Go by example +Go has different learning paths of which one is [Go by example](https://gobyexample.com/). + +Here are all files with notations and more. + +How to use the files: +```bash +# compile and run in the folder +go run .go + +# build binary from file; after that run binary +go build .go +./ +``` + + +The code and structure is work of Mark McGranaghan and licensed under a Creative Commons Attribution 3.0 Unported License. +The Go Gopher is copyright Renée French. The original repository can be found on [Github](https://github.com/mmcgrana/gobyexample) \ No newline at end of file