starting go by example

This commit is contained in:
WieErWill 2021-12-31 15:36:34 +01:00
parent bafba8a7b6
commit afe165089d
11 changed files with 340 additions and 0 deletions

View File

@ -0,0 +1,7 @@
package main
import "fmt"
func main() {
fmt.Println("hello world")
}

View File

@ -0,0 +1,36 @@
package main
import "fmt"
// Maps are Gos 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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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 its 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))
}

34
5-go-by-example/5-for.go Normal file
View File

@ -0,0 +1,34 @@
package main
import "fmt"
// for is Gos 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)
}
}

View File

@ -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")
}
}

View File

@ -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")
}

View File

@ -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)
}

View File

@ -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)
}

18
5-go-by-example/readme.md Normal file
View File

@ -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 <filename>.go
# build binary from file; after that run binary
go build <filename>.go
./<filename>
```
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)