├── .gitignore ├── README.md ├── day1 ├── lab1 │ ├── CtoF.go │ ├── assign.go │ └── curly.go ├── lab2 │ ├── aPackage.go │ ├── arrayMap.go │ ├── error.go │ └── useAP.go ├── lab3 │ ├── random.go │ ├── randomStructure.go │ └── runes.go ├── s1 │ ├── hw.go │ └── hwNoOS.go ├── s2 │ ├── aPackage.go │ ├── arrays.go │ ├── dataTypes.go │ ├── errorType.go │ ├── failMap.go │ ├── functions.go │ ├── loops.go │ ├── maps.go │ ├── slices.go │ ├── unglyHW.go │ ├── userIO.go │ ├── usingAPackage.go │ ├── usingDefer.go │ └── usingFlag.go └── s3 │ ├── goStrings.go │ ├── runes.go │ └── struct.go └── day2 ├── arrayToSlice.go ├── lab4 ├── closeNilChannel.go ├── monitor.go ├── pipelines.go ├── readWriteCh.go ├── syncVarGo.go ├── varGo.go └── writeCh.go ├── lab5 ├── concTCP.go ├── tC.go ├── tS.go ├── uC.go └── uS.go ├── lab6 ├── DNS.go ├── NSrecords.go ├── advancedWebClient.go ├── calcPi.go ├── switch.go ├── usingTime.go ├── webClient.go └── www.go ├── s4 ├── create10.go ├── create10sleep.go ├── pipelines.go ├── readWriteCh.go ├── usingSync.go └── writeCh.go ├── s5 ├── concTCP.go ├── tC.go ├── tS.go ├── uC.go └── uS.go └── s6 ├── httpClient.go └── www.go /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## An Introduction to Go for Systems Programmers and Web Developers 2 | 3 | ### September 17 & 18, 2018 4 | 5 | https://www.safaribooksonline.com/live-training/courses/an-introduction-to-go-for-systems-programmers-and-web-developers/0636920213956/ 6 | 7 | ### November 12 & 13, 2018 8 | 9 | https://www.safaribooksonline.com/live-training/courses/an-introduction-to-go-for-systems-programmers-and-web-developers/0636920214069/ 10 | -------------------------------------------------------------------------------- /day1/lab1/CtoF.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | var temperature float64 9 | var t int 10 | 11 | for { 12 | fmt.Println("Give me the temperature: ") 13 | fmt.Scanf("%f", &temperature) 14 | fmt.Println("0 for Celsius - 1 for Fahrenheit - anything else to exit") 15 | fmt.Scanf("%d", &t) 16 | if t == 0 { 17 | fmt.Println("Converting Celsius to Fahrenheit.") 18 | f := 1.8*temperature + 32 19 | fmt.Printf("%.2f C is %.3f F.\n", temperature, f) 20 | } else if t == 1 { 21 | fmt.Println("Converting Fahrenheit to Celsius.") 22 | c := (temperature - 32) * 5 / 9 23 | fmt.Printf("%.2f F is %.3f C.\n", temperature, c) 24 | } else { 25 | return 26 | } 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /day1/lab1/assign.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | t := 1 7 | t = 10 8 | fmt.Println(t) 9 | } 10 | -------------------------------------------------------------------------------- /day1/lab1/curly.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | fmt.Println("Go has strict rules for curly braces!") 9 | } 10 | 11 | -------------------------------------------------------------------------------- /day1/lab2/aPackage.go: -------------------------------------------------------------------------------- 1 | package aPackage 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func A() { 8 | fmt.Println("This is function A! - CHANGED") 9 | } 10 | 11 | func B() { 12 | fmt.Println("privateConstant:", privateConstant) 13 | } 14 | 15 | const MyConstant = 123 16 | const privateConstant = 21 17 | 18 | 19 | -------------------------------------------------------------------------------- /day1/lab2/arrayMap.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | func main() { 9 | anArray := [4]int{1, -2, -1, 0} 10 | aMap := make(map[string]int) 11 | 12 | length := len(anArray) 13 | for i := 0; i < length; i++ { 14 | fmt.Printf("%s ", strconv.Itoa(i)) 15 | aMap[strconv.Itoa(i)] = anArray[i] 16 | } 17 | fmt.Printf("\n") 18 | 19 | for key, value := range aMap { 20 | fmt.Printf("%s: %d\n", key, value) 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /day1/lab2/error.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | ) 7 | 8 | func returnError(a, b int) error { 9 | if a == b { 10 | err := errors.New("Error in returnError() function!") 11 | return err 12 | } else { 13 | return nil 14 | } 15 | } 16 | 17 | func main() { 18 | err := returnError(1, 2) 19 | if err == nil { 20 | fmt.Println("returnError() ended normally!") 21 | } else { 22 | fmt.Println(err) 23 | } 24 | 25 | err = returnError(1, 1) 26 | if err.Error() == "Error in returnError() function!" { 27 | fmt.Println("!!") 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /day1/lab2/useAP.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "aPackage" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | fmt.Println("Using aPackage!") 10 | aPackage.A() 11 | aPackage.B() 12 | fmt.Println(aPackage.MyConstant) 13 | } 14 | 15 | -------------------------------------------------------------------------------- /day1/lab3/random.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "os" 7 | "strconv" 8 | "time" 9 | ) 10 | 11 | func random(min, max int) int { 12 | return rand.Intn(max-min) + min 13 | } 14 | 15 | func main() { 16 | MIN := 0 17 | MAX := 0 18 | TOTAL := 0 19 | 20 | if len(os.Args) > 3 { 21 | temp, err := strconv.Atoi(os.Args[1]) 22 | if err != nil { 23 | fmt.Println(err) 24 | os.Exit(-1) 25 | } 26 | MIN = temp 27 | 28 | temp, err = strconv.Atoi(os.Args[2]) 29 | if err != nil { 30 | fmt.Println(err) 31 | os.Exit(-1) 32 | } 33 | MAX = temp 34 | 35 | temp, err = strconv.Atoi(os.Args[3]) 36 | if err != nil { 37 | fmt.Println(err) 38 | os.Exit(-1) 39 | } 40 | TOTAL = temp 41 | } else { 42 | fmt.Println("Usage:", os.Args[0], "MIN MAX TOTAL") 43 | os.Exit(-1) 44 | } 45 | 46 | rand.Seed(time.Now().Unix()) 47 | for i := 0; i < TOTAL; i++ { 48 | myrand := random(MIN, MAX) 49 | fmt.Print(myrand) 50 | fmt.Print(" ") 51 | } 52 | fmt.Println() 53 | } 54 | 55 | -------------------------------------------------------------------------------- /day1/lab3/randomStructure.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "time" 7 | ) 8 | 9 | type XYZ struct { 10 | seed int64 11 | first int 12 | second int 13 | } 14 | 15 | func random(min, max int) int { 16 | return rand.Intn(max-min) + min 17 | } 18 | 19 | func main() { 20 | data := []XYZ{} 21 | for i := 0; i < 100; i++ { 22 | seed := time.Now().UnixNano() 23 | rand.Seed(seed) 24 | first := random(0, 100) 25 | second := random(0, 100) 26 | temp := XYZ{seed, first, second} 27 | data = append(data, temp) 28 | } 29 | fmt.Println(data) 30 | } 31 | 32 | -------------------------------------------------------------------------------- /day1/lab3/runes.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | const r1 = '€' 9 | fmt.Println("(int32) r1:", r1) 10 | fmt.Printf("(HEX) r1: %x\n", r1) 11 | fmt.Printf("(as a String) r1: %s\n", r1) 12 | fmt.Printf("(as a character) r1: %c\n", r1) 13 | fmt.Println("A string is a collection of runes:", []byte("Mihalis")) 14 | aString := []byte("Mihalis") 15 | for x, y := range aString { 16 | fmt.Println(x, y) 17 | fmt.Printf("Char: %c\n", aString[x]) 18 | } 19 | fmt.Printf("%s\n", aString) 20 | } 21 | 22 | -------------------------------------------------------------------------------- /day1/s1/hw.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func main() { 9 | fmt.Println("Hello World!") 10 | } 11 | -------------------------------------------------------------------------------- /day1/s1/hwNoOS.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | fmt.Println("Hello World!") 9 | } 10 | -------------------------------------------------------------------------------- /day1/s2/aPackage.go: -------------------------------------------------------------------------------- 1 | package aPackage 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func A() { 8 | fmt.Println("This is function A!") 9 | } 10 | 11 | func B() { 12 | fmt.Println("privateConstant:", privateConstant) 13 | } 14 | 15 | const MyConstant = 123 16 | const privateConstant = 21 17 | 18 | 19 | -------------------------------------------------------------------------------- /day1/s2/arrays.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | myArray := [4]int{1, 2, 4, -4} 9 | length := len(myArray) 10 | 11 | for i := 0; i < length; i++ { 12 | fmt.Printf("%d ", myArray[i]) 13 | } 14 | fmt.Printf("\n") 15 | 16 | otherArray := [...]int{-2, 6, 7, 8} 17 | for _, number := range otherArray { 18 | fmt.Printf("%d ", number) 19 | } 20 | fmt.Println() 21 | fmt.Printf("\n") 22 | 23 | twoD := [3][3]int{ 24 | {1, 2, 3}, 25 | {4, 5, 6}, 26 | {7, 8, 9}} 27 | 28 | twoD[1][2] = 15 29 | for _, number := range twoD { 30 | for _, other := range number { 31 | fmt.Printf("%d ", other) 32 | } 33 | } 34 | 35 | fmt.Printf("\n") 36 | } 37 | 38 | -------------------------------------------------------------------------------- /day1/s2/dataTypes.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var m, n float64 7 | var c complex128 8 | x := 12 9 | 10 | fmt.Println(x) 11 | fmt.Printf("Type of c: %T\n", c) 12 | fmt.Printf("Type of x: %T\n", x) 13 | fmt.Println("m, n:", m, n) 14 | } 15 | -------------------------------------------------------------------------------- /day1/s2/errorType.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "log" 7 | ) 8 | 9 | func division(x, y int) (int, error) { 10 | if y == 0 { 11 | return 0, errors.New("Cannot divide by zero!") 12 | } 13 | return x / y, nil 14 | } 15 | 16 | func main() { 17 | result, err := division(2, 2) 18 | if err != nil { 19 | fmt.Println(err) 20 | return 21 | } 22 | fmt.Println("The result is", result) 23 | 24 | result2, err := division(2, 0) 25 | if err != nil { 26 | log.Fatal(err) 27 | } 28 | 29 | fmt.Println("The result is", result2) 30 | } 31 | 32 | -------------------------------------------------------------------------------- /day1/s2/failMap.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | aMap := map[string]int{} 9 | aMap["test"] = 1 10 | fmt.Println(aMap) 11 | aMap = nil 12 | fmt.Println(aMap) 13 | aMap["test"] = 1 14 | } 15 | -------------------------------------------------------------------------------- /day1/s2/functions.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func unnamedMinMax(x, y int) (int, int) { 8 | if x > y { 9 | min := y 10 | max := x 11 | return min, max 12 | } else { 13 | min := x 14 | max := y 15 | return min, max 16 | } 17 | } 18 | 19 | func minMax(x, y int) (min, max int) { 20 | if x > y { 21 | min = y 22 | max = x 23 | } else { 24 | min = x 25 | max = y 26 | } 27 | return min, max 28 | } 29 | 30 | func namedMinMax(x, y int) (min, max int) { 31 | if x > y { 32 | min = y 33 | max = x 34 | } else { 35 | min = x 36 | max = y 37 | } 38 | return 39 | } 40 | 41 | func sort(x, y int) (int, int) { 42 | if x > y { 43 | return x, y 44 | } else { 45 | return y, x 46 | } 47 | } 48 | 49 | func main() { 50 | y := 4 51 | square := func(s int) int { 52 | return s * s 53 | } 54 | fmt.Println("The square of", y, "is", square(y)) 55 | 56 | square = func(s int) int { 57 | return s + s 58 | } 59 | fmt.Println("The square of", y, "is", square(y)) 60 | 61 | fmt.Println(minMax(15, 6)) 62 | fmt.Println(namedMinMax(15, 6)) 63 | min, max := namedMinMax(12, -1) 64 | fmt.Println(min, max) 65 | } 66 | 67 | -------------------------------------------------------------------------------- /day1/s2/loops.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | for i := 0; i < 100; i++ { 9 | if i%20 == 0 { 10 | continue 11 | } 12 | if i == 95 { 13 | break 14 | } 15 | fmt.Print(i, " ") 16 | } 17 | 18 | fmt.Println() 19 | i := 10 20 | for { 21 | if i < 0 { 22 | break 23 | } 24 | fmt.Print(i, " ") 25 | i-- 26 | } 27 | 28 | fmt.Println() 29 | i = 0 30 | anExpr := true 31 | for ok := true; ok; ok = anExpr { 32 | if i > 10 { 33 | anExpr = false 34 | } 35 | 36 | fmt.Print(i, " ") 37 | i++ 38 | } 39 | fmt.Println() 40 | fmt.Println("Like while:") 41 | 42 | ok := true 43 | i = 0 44 | for ok { 45 | if i > 10 { 46 | ok = false 47 | } 48 | 49 | fmt.Print(i, " ") 50 | i++ 51 | } 52 | fmt.Println() 53 | 54 | anArray := [5]int{0, 1, -1, 2, -2} 55 | for i, v := range anArray { 56 | fmt.Println("ind:", i, "v: ", v) 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /day1/s2/maps.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | aMap := make(map[string]int) 9 | aMap["Mon"] = 0 10 | aMap["Tue"] = 1 11 | aMap["Wed"] = 2 12 | aMap["Thu"] = 3 13 | aMap["Fri"] = 4 14 | aMap["Sat"] = 5 15 | aMap["Sun"] = 6 16 | fmt.Printf("Sunday is the %dth day of the week.\n", aMap["Sun"]) 17 | 18 | ok := true 19 | _, ok = aMap["Tuesday"] 20 | if ok { 21 | fmt.Printf("The Tuesday key exists!\n") 22 | } else { 23 | fmt.Printf("The Tuesday key does not exist!\n") 24 | } 25 | 26 | count := 0 27 | for key, _ := range aMap { 28 | count++ 29 | fmt.Printf("%s ", key) 30 | } 31 | fmt.Printf("\n") 32 | fmt.Printf("The aMap has %d elements\n", count) 33 | 34 | count = 0 35 | delete(aMap, "Fri") 36 | for _, _ = range aMap { 37 | count++ 38 | } 39 | fmt.Printf("The aMap has now %d elements\n", count) 40 | 41 | count = 0 42 | for _ = range aMap { 43 | count++ 44 | } 45 | fmt.Printf("The aMap has now %d elements\n", count) 46 | 47 | anotherMap := map[string]int{ 48 | "One": 1, 49 | "Two": 2, 50 | "Three": 3, 51 | "Four": 4, 52 | } 53 | fmt.Println(anotherMap) 54 | } 55 | -------------------------------------------------------------------------------- /day1/s2/slices.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func negative(x []int) { 8 | for i, k := range x { 9 | x[i] = -k 10 | } 11 | } 12 | 13 | func printSlice(x []int) { 14 | for _, number := range x { 15 | fmt.Printf("%d ", number) 16 | } 17 | fmt.Println() 18 | } 19 | 20 | func main() { 21 | s := []int{0, 14, 5, 0, 7, 19} 22 | printSlice(s) 23 | negative(s) 24 | printSlice(s) 25 | 26 | fmt.Printf("Before. Cap: %d, length: %d\n", cap(s), len(s)) 27 | 28 | s = append(s, -100) 29 | fmt.Printf("After. Cap: %d, length: %d\n", cap(s), len(s)) 30 | 31 | printSlice(s) 32 | 33 | anotherSlice := make([]int, 14) 34 | fmt.Printf("A slice with 14 elements: ") 35 | printSlice(anotherSlice) 36 | } 37 | -------------------------------------------------------------------------------- /day1/s2/unglyHW.go: -------------------------------------------------------------------------------- 1 | package main 2 | import 3 | "fmt" 4 | // This is a demonstrative comment! 5 | func main() { 6 | fmt.Println("Hello World!") 7 | } 8 | 9 | -------------------------------------------------------------------------------- /day1/s2/userIO.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Print("Give me a number: ") 7 | var aNumber int 8 | fmt.Scanln(&aNumber) 9 | fmt.Println("Your number is:", aNumber) 10 | 11 | // Read a string 12 | fmt.Print("Enter your Name: ") 13 | var name string 14 | fmt.Scanln(&name) 15 | fmt.Println("Your name is:", name) 16 | fmt.Print("Your name is: ", name, "\n") 17 | fmt.Printf("Your name is: %s\n", name) 18 | 19 | // Print its type 20 | fmt.Printf("The type of variable name is %T\n", name) 21 | // Define the decimal points of the output 22 | fmt.Printf("%.4f\n", 123.1231231) 23 | fmt.Println(123.1231231) 24 | } 25 | 26 | -------------------------------------------------------------------------------- /day1/s2/usingAPackage.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "aPackage" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | fmt.Println("Using aPackage!") 10 | aPackage.A() 11 | aPackage.B() 12 | fmt.Println(aPackage.MyConstant) 13 | } 14 | 15 | -------------------------------------------------------------------------------- /day1/s2/usingDefer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | // 2 1 0 8 | func a1() { 9 | for i := 0; i < 3; i++ { 10 | defer fmt.Print(i, " ") 11 | } 12 | } 13 | 14 | // 3 3 3 15 | func a2() { 16 | for i := 0; i < 3; i++ { 17 | defer func() { 18 | fmt.Print(i, " ") 19 | }() 20 | } 21 | } 22 | 23 | // 2 1 0 24 | func a3() { 25 | for i := 0; i < 3; i++ { 26 | defer func(n int) { 27 | fmt.Print(n, " ") 28 | }(i) 29 | } 30 | } 31 | 32 | func main() { 33 | a1() 34 | fmt.Println() 35 | a2() 36 | fmt.Println() 37 | a3() 38 | fmt.Println() 39 | } 40 | 41 | -------------------------------------------------------------------------------- /day1/s2/usingFlag.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | minusO := flag.Bool("o", false, "o") 10 | minusC := flag.Bool("c", false, "c") 11 | minusK := flag.Int("k", 0, "an int") 12 | 13 | flag.Parse() 14 | 15 | fmt.Println("-o:", *minusO) 16 | fmt.Println("-c:", *minusC) 17 | fmt.Println("-k:", *minusK) 18 | 19 | for index, val := range flag.Args() { 20 | fmt.Println(index, ":", val) 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /day1/s3/goStrings.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | const sLiteral = "\x99\x42\x32\x55\x50\x35\x23\x50\x29\x9c" 9 | fmt.Println(sLiteral) 10 | fmt.Printf("x: %x\n", sLiteral) 11 | 12 | fmt.Printf("sLiteral length: %d\n", len(sLiteral)) 13 | 14 | for i := 0; i < len(sLiteral); i++ { 15 | fmt.Printf("%x ", sLiteral[i]) 16 | } 17 | fmt.Println() 18 | 19 | fmt.Printf("q: %q\n", sLiteral) 20 | fmt.Printf("+q: %+q\n", sLiteral) 21 | fmt.Printf(" x: % x\n", sLiteral) 22 | 23 | fmt.Printf("s: As a string: %s\n", sLiteral) 24 | 25 | s2 := "€£³" 26 | for x, y := range s2 { 27 | fmt.Printf("%#U starts at byte position %d\n", y, x) 28 | } 29 | 30 | fmt.Printf("s2 length: %d\n", len(s2)) 31 | 32 | const s3 = "ab12AB" 33 | fmt.Println("s3:", s3) 34 | fmt.Printf("x: % x\n", s3) 35 | 36 | fmt.Printf("s3 length: %d\n", len(s3)) 37 | 38 | for i := 0; i < len(s3); i++ { 39 | fmt.Printf("%x ", s3[i]) 40 | } 41 | fmt.Println() 42 | } 43 | 44 | -------------------------------------------------------------------------------- /day1/s3/runes.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | const r1 = '€' 7 | fmt.Println("(int32) r1:", r1) 8 | fmt.Printf("(HEX) r1: %x\n", r1) 9 | fmt.Printf("(as String) r1: %s\n", r1) 10 | 11 | fmt.Printf("(as character) r1: %c\n", r1) 12 | fmt.Println("A string is a collection of runes:", []byte("Mihalis")) 13 | 14 | aString := []byte("Mihalis") 15 | for x, y := range aString { 16 | fmt.Println(x, y) 17 | fmt.Printf("Char: %c\n", aString[x]) 18 | } 19 | 20 | fmt.Printf("%s\n", aString) 21 | } 22 | 23 | -------------------------------------------------------------------------------- /day1/s3/struct.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | type XYZ struct { 8 | X int 9 | Y int 10 | Z int 11 | } 12 | 13 | func main() { 14 | var s1 XYZ 15 | fmt.Println(s1.Y, s1.Z) 16 | 17 | p1 := XYZ{23, 12, -2} 18 | p2 := XYZ{Z: 12, Y: 13} 19 | fmt.Println(p1) 20 | fmt.Println("p2:", p2) 21 | 22 | pSlice := [4]XYZ{} 23 | pSlice[2] = p1 24 | pSlice[0] = p2 25 | fmt.Println(pSlice) 26 | p2 = XYZ{1, 2, 3} 27 | fmt.Println(pSlice) 28 | } 29 | 30 | -------------------------------------------------------------------------------- /day2/arrayToSlice.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | s := []int{1, 2, 3} 9 | a := [3]int{4, 5, 6} 10 | 11 | ref := a[:] 12 | fmt.Println(ref) 13 | 14 | t := append(s, ref...) 15 | fmt.Println(t) 16 | 17 | s = append(s, ref...) 18 | fmt.Println(s) 19 | } 20 | -------------------------------------------------------------------------------- /day2/lab4/closeNilChannel.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var c chan string 7 | fmt.Println(c) 8 | // close(c) 9 | } 10 | 11 | -------------------------------------------------------------------------------- /day2/lab4/monitor.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "os" 7 | "strconv" 8 | "sync" 9 | "time" 10 | ) 11 | 12 | var readValue = make(chan int) 13 | var writeValue = make(chan int) 14 | 15 | func set(newValue int) { 16 | writeValue <- newValue 17 | } 18 | 19 | func read() int { 20 | return <-readValue 21 | } 22 | 23 | func monitor() { 24 | var value int 25 | for { 26 | select { 27 | case newValue := <-writeValue: 28 | value = newValue 29 | fmt.Printf("%d ", value) 30 | case readValue <- value: 31 | } 32 | } 33 | } 34 | 35 | func main() { 36 | if len(os.Args) != 2 { 37 | fmt.Println("Please give an integer!") 38 | return 39 | } 40 | n, err := strconv.Atoi(os.Args[1]) 41 | if err != nil { 42 | fmt.Println(err) 43 | return 44 | } 45 | 46 | fmt.Printf("Going to create %d random numbers.\n", n) 47 | rand.Seed(time.Now().Unix()) 48 | go monitor() 49 | var w sync.WaitGroup 50 | 51 | for r := 0; r < n; r++ { 52 | w.Add(1) 53 | go func() { 54 | defer w.Done() 55 | set(rand.Intn(10 * n)) 56 | }() 57 | } 58 | w.Wait() 59 | fmt.Printf("\nLast value: %d\n", read()) 60 | } 61 | -------------------------------------------------------------------------------- /day2/lab4/pipelines.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "os" 7 | "strconv" 8 | "time" 9 | ) 10 | 11 | var CLOSEA = false 12 | var DATA = make(map[int]bool) 13 | 14 | func random(min, max int) int { 15 | return rand.Intn(max-min) + min 16 | } 17 | 18 | func first(min, max int, out chan<- int) { 19 | for { 20 | if CLOSEA { 21 | close(out) 22 | return 23 | } 24 | out <- random(min, max) 25 | } 26 | } 27 | 28 | func second(out chan<- int, in <-chan int) { 29 | for x := range in { 30 | fmt.Print(x, " ") 31 | _, ok := DATA[x] 32 | if ok { 33 | CLOSEA = true 34 | } else { 35 | DATA[x] = true 36 | out <- x 37 | } 38 | } 39 | fmt.Println() 40 | close(out) 41 | } 42 | 43 | func third(in <-chan int) { 44 | var sum int 45 | sum = 0 46 | for x2 := range in { 47 | sum = sum + x2 48 | } 49 | fmt.Printf("The sum of the random numbers is %d\n", sum) 50 | } 51 | 52 | func main() { 53 | if len(os.Args) != 3 { 54 | fmt.Println("Need two integer parameters!") 55 | os.Exit(1) 56 | } 57 | 58 | n1, _ := strconv.Atoi(os.Args[1]) 59 | n2, _ := strconv.Atoi(os.Args[2]) 60 | 61 | if n1 >= n2 { 62 | fmt.Printf("%d should be smaller than %d\n", n1, n2) 63 | return 64 | } 65 | 66 | rand.Seed(time.Now().UnixNano()) 67 | A := make(chan int) 68 | B := make(chan int) 69 | 70 | go first(n1, n2, A) 71 | go second(B, A) 72 | third(B) 73 | } 74 | 75 | -------------------------------------------------------------------------------- /day2/lab4/readWriteCh.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func writeChannel(c chan<- int, x int) { 9 | fmt.Println(x) 10 | c <- x 11 | close(c) 12 | fmt.Println(x) 13 | } 14 | 15 | func main() { 16 | c := make(chan int) 17 | go writeChannel(c, 10) 18 | time.Sleep(2 * time.Second) 19 | fmt.Println("Read:", <-c) 20 | time.Sleep(2 * time.Second) 21 | 22 | _, ok := <-c 23 | if ok { 24 | fmt.Println("Channel is open!") 25 | } else { 26 | fmt.Println("Channel is closed!") 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /day2/lab4/syncVarGo.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "sync" 7 | ) 8 | 9 | func main() { 10 | n := flag.Int("n", 20, "Number of goroutines") 11 | flag.Parse() 12 | count := *n 13 | fmt.Printf("Creating %d goroutines.\n", count) 14 | 15 | var waitGroup sync.WaitGroup 16 | 17 | // waitGroup.Add(count) 18 | 19 | // fmt.Printf("%#v\n", waitGroup) 20 | for i := 0; i < count; i++ { 21 | waitGroup.Add(1) 22 | go func(x int) { 23 | defer waitGroup.Done() 24 | fmt.Printf("%d ", x) 25 | }(i) 26 | } 27 | 28 | // fmt.Printf("%#v\n", waitGroup) 29 | waitGroup.Wait() 30 | fmt.Println("\nExiting...") 31 | } 32 | 33 | -------------------------------------------------------------------------------- /day2/lab4/varGo.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "time" 7 | ) 8 | 9 | func main() { 10 | n := flag.Int("n", 20, "# goroutines") 11 | flag.Parse() 12 | count := *n 13 | fmt.Printf("Creating %d goroutines.\n", count) 14 | 15 | for i := 0; i < count; i++ { 16 | go func(x int) { 17 | fmt.Printf("%d ", x) 18 | }(i) 19 | } 20 | 21 | time.Sleep(time.Second) 22 | fmt.Println("\nExiting...") 23 | } 24 | 25 | -------------------------------------------------------------------------------- /day2/lab4/writeCh.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func wCh(c chan<- int, x int) { 9 | fmt.Println(x) 10 | c <- x 11 | close(c) 12 | } 13 | 14 | func main() { 15 | c := make(chan int) 16 | go wCh(c, 10) 17 | time.Sleep(2 * time.Second) 18 | fmt.Println("*", <-c) 19 | fmt.Println("Read:", <-c) 20 | } 21 | 22 | -------------------------------------------------------------------------------- /day2/lab5/concTCP.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "math/rand" 7 | "net" 8 | "os" 9 | "strconv" 10 | "strings" 11 | "time" 12 | ) 13 | 14 | const MIN = 1 15 | const MAX = 100 16 | 17 | func random() int { 18 | return rand.Intn(MAX-MIN) + MIN 19 | } 20 | 21 | func handleConnection(c net.Conn) { 22 | fmt.Printf("Serving %s\n", c.RemoteAddr().String()) 23 | for { 24 | netData, err := bufio.NewReader(c).ReadString('\n') 25 | if err != nil { 26 | fmt.Println(err) 27 | return 28 | } 29 | 30 | temp := strings.TrimSpace(string(netData)) 31 | if temp == "STOP" { 32 | break 33 | } 34 | 35 | result := strconv.Itoa(random()) + "\n" 36 | c.Write([]byte(string(result))) 37 | } 38 | c.Close() 39 | } 40 | 41 | func main() { 42 | arguments := os.Args 43 | if len(arguments) == 1 { 44 | fmt.Println("Please provide a port number!") 45 | return 46 | } 47 | 48 | PORT := ":" + arguments[1] 49 | l, err := net.Listen("tcp4", PORT) 50 | if err != nil { 51 | fmt.Println(err) 52 | return 53 | } 54 | defer l.Close() 55 | rand.Seed(time.Now().Unix()) 56 | 57 | for { 58 | c, err := l.Accept() 59 | if err != nil { 60 | fmt.Println(err) 61 | return 62 | } 63 | go handleConnection(c) 64 | } 65 | } 66 | 67 | -------------------------------------------------------------------------------- /day2/lab5/tC.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "net" 7 | "os" 8 | "strings" 9 | ) 10 | 11 | func main() { 12 | arguments := os.Args 13 | if len(arguments) == 1 { 14 | fmt.Println("Please provide host:port.") 15 | return 16 | } 17 | 18 | CONNECT := arguments[1] 19 | c, err := net.Dial("tcp", CONNECT) 20 | 21 | if err != nil { 22 | fmt.Println(err) 23 | return 24 | } 25 | 26 | for { 27 | reader := bufio.NewReader(os.Stdin) 28 | fmt.Print(">> ") 29 | text, _ := reader.ReadString('\n') 30 | fmt.Fprintf(c, text+"\n") 31 | 32 | message, _ := bufio.NewReader(c).ReadString('\n') 33 | fmt.Print("->: " + message) 34 | if strings.TrimSpace(string(text)) == "STOP" { 35 | fmt.Println("client exiting...") 36 | return 37 | } 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /day2/lab5/tS.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "net" 7 | "os" 8 | "strings" 9 | "time" 10 | ) 11 | 12 | func main() { 13 | arguments := os.Args 14 | if len(arguments) == 1 { 15 | fmt.Println("Please provide port number") 16 | return 17 | } 18 | 19 | PORT := ":" + arguments[1] 20 | l, err := net.Listen("tcp", PORT) 21 | if err != nil { 22 | fmt.Println(err) 23 | return 24 | } 25 | defer l.Close() 26 | 27 | c, err := l.Accept() 28 | if err != nil { 29 | fmt.Println(err) 30 | return 31 | } 32 | 33 | for { 34 | netData, err := bufio.NewReader(c).ReadString('\n') 35 | if err != nil { 36 | fmt.Println(err) 37 | return 38 | } 39 | if strings.TrimSpace(string(netData)) == "STOP" { 40 | fmt.Println("TCP server!") 41 | return 42 | } 43 | 44 | fmt.Print("-> ", string(netData)) 45 | t := time.Now() 46 | myTime := t.Format(time.RFC3339) + "\n" 47 | c.Write([]byte(myTime)) 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /day2/lab5/uC.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "net" 7 | "os" 8 | "strings" 9 | ) 10 | 11 | func main() { 12 | arguments := os.Args 13 | if len(arguments) == 1 { 14 | fmt.Println("Please provide a host:port string") 15 | return 16 | } 17 | CONNECT := arguments[1] 18 | s, err := net.ResolveUDPAddr("udp4", CONNECT) 19 | c, err := net.DialUDP("udp4", nil, s) 20 | if err != nil { 21 | fmt.Println(err) 22 | return 23 | } 24 | 25 | fmt.Printf("The UDP server is %s\n", c.RemoteAddr().String()) 26 | defer c.Close() 27 | 28 | for { 29 | reader := bufio.NewReader(os.Stdin) 30 | fmt.Print(">> ") 31 | text, _ := reader.ReadString('\n') 32 | data := []byte(text + "\n") 33 | _, err = c.Write(data) 34 | if strings.TrimSpace(string(data)) == "STOP" { 35 | fmt.Println("Exiting UDP client!") 36 | return 37 | } 38 | if err != nil { 39 | fmt.Println(err) 40 | return 41 | } 42 | buffer := make([]byte, 1024) 43 | n, _, err := c.ReadFromUDP(buffer) 44 | if err != nil { 45 | fmt.Println(err) 46 | return 47 | } 48 | fmt.Printf("Reply: %s\n", string(buffer[0:n])) 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /day2/lab5/uS.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "net" 7 | "os" 8 | "strconv" 9 | "strings" 10 | "time" 11 | ) 12 | 13 | func random(min, max int) int { 14 | return rand.Intn(max-min) + min 15 | } 16 | 17 | func main() { 18 | arguments := os.Args 19 | if len(arguments) == 1 { 20 | fmt.Println("Provide a port number!") 21 | return 22 | } 23 | 24 | PORT := ":" + arguments[1] 25 | 26 | s, err := net.ResolveUDPAddr("udp4", PORT) 27 | if err != nil { 28 | fmt.Println(err) 29 | return 30 | } 31 | 32 | connection, err := net.ListenUDP("udp4", s) 33 | if err != nil { 34 | fmt.Println(err) 35 | return 36 | } 37 | defer connection.Close() 38 | 39 | buffer := make([]byte, 1024) 40 | rand.Seed(time.Now().Unix()) 41 | for { 42 | n, addr, err := connection.ReadFromUDP(buffer) 43 | fmt.Print("-> ", string(buffer[0:n-1])) 44 | 45 | if strings.TrimSpace(string(buffer[0:n])) == "STOP" { 46 | fmt.Println("Exiting UDP server!") 47 | return 48 | } 49 | 50 | data := []byte(strconv.Itoa(random(1, 1001))) 51 | fmt.Printf("data: %s\n", string(data)) 52 | _, err = connection.WriteToUDP(data, addr) 53 | if err != nil { 54 | fmt.Println(err) 55 | return 56 | } 57 | } 58 | } 59 | 60 | -------------------------------------------------------------------------------- /day2/lab6/DNS.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | "os" 7 | ) 8 | 9 | func lookIP(address string) ([]string, error) { 10 | hosts, err := net.LookupAddr(address) 11 | if err != nil { 12 | return nil, err 13 | } 14 | return hosts, nil 15 | } 16 | 17 | func lookHostname(hostname string) ([]string, error) { 18 | IPs, err := net.LookupHost(hostname) 19 | if err != nil { 20 | return nil, err 21 | } 22 | return IPs, nil 23 | } 24 | 25 | func main() { 26 | arguments := os.Args 27 | if len(arguments) == 1 { 28 | fmt.Println("Please provide an argument!") 29 | return 30 | } 31 | 32 | input := arguments[1] 33 | IPaddress := net.ParseIP(input) 34 | 35 | if IPaddress == nil { 36 | IPs, err := lookHostname(input) 37 | if err == nil { 38 | for _, singleIP := range IPs { 39 | fmt.Println(singleIP) 40 | } 41 | } 42 | } else { 43 | hosts, err := lookIP(input) 44 | if err == nil { 45 | for _, hostname := range hosts { 46 | fmt.Println(hostname) 47 | } 48 | } 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /day2/lab6/NSrecords.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | "os" 7 | ) 8 | 9 | func main() { 10 | arguments := os.Args 11 | if len(arguments) == 1 { 12 | fmt.Println("Need a domain name!") 13 | return 14 | } 15 | 16 | domain := arguments[1] 17 | NSs, err := net.LookupNS(domain) 18 | if err != nil { 19 | fmt.Println(err) 20 | return 21 | } 22 | 23 | for _, NS := range NSs { 24 | fmt.Println(NS.Host) 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /day2/lab6/advancedWebClient.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "net/http/httputil" 7 | "net/url" 8 | "os" 9 | "path/filepath" 10 | "strings" 11 | "time" 12 | ) 13 | 14 | func main() { 15 | if len(os.Args) != 2 { 16 | fmt.Printf("Usage: %s URL\n", filepath.Base(os.Args[0])) 17 | return 18 | } 19 | 20 | URL, err := url.Parse(os.Args[1]) 21 | if err != nil { 22 | fmt.Println("Error in parsing:", err) 23 | return 24 | } 25 | 26 | c := &http.Client{ 27 | Timeout: 15 * time.Second, 28 | } 29 | request, err := http.NewRequest("GET", URL.String(), nil) 30 | if err != nil { 31 | fmt.Println("Get:", err) 32 | return 33 | } 34 | 35 | httpData, err := c.Do(request) 36 | if err != nil { 37 | fmt.Println("Error in Do():", err) 38 | return 39 | } 40 | 41 | fmt.Println("Status code:", httpData.Status) 42 | header, _ := httputil.DumpResponse(httpData, false) 43 | fmt.Print(string(header)) 44 | 45 | contentType := httpData.Header.Get("Content-Type") 46 | characterSet := strings.SplitAfter(contentType, "charset=") 47 | if len(characterSet) > 1 { 48 | fmt.Println("Character Set:", characterSet[1]) 49 | } 50 | 51 | if httpData.ContentLength == -1 { 52 | fmt.Println("ContentLength is unknown!") 53 | } else { 54 | fmt.Println("ContentLength:", httpData.ContentLength) 55 | } 56 | 57 | length := 0 58 | var buffer [1024]byte 59 | r := httpData.Body 60 | for { 61 | n, err := r.Read(buffer[0:]) 62 | if err != nil { 63 | fmt.Println(err) 64 | break 65 | } 66 | length = length + n 67 | } 68 | fmt.Println("Calculated response data length:", length) 69 | } 70 | -------------------------------------------------------------------------------- /day2/lab6/calcPi.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | "math/big" 7 | "os" 8 | "strconv" 9 | ) 10 | 11 | var precision uint = 0 12 | 13 | func Pi(accuracy uint) *big.Float { 14 | k := 0 15 | pi := new(big.Float).SetPrec(precision).SetFloat64(0) 16 | k1k2k3 := new(big.Float).SetPrec(precision).SetFloat64(0) 17 | k4k5k6 := new(big.Float).SetPrec(precision).SetFloat64(0) 18 | temp := new(big.Float).SetPrec(precision).SetFloat64(0) 19 | minusOne := new(big.Float).SetPrec(precision).SetFloat64(-1) 20 | total := new(big.Float).SetPrec(precision).SetFloat64(0) 21 | 22 | two2Six := math.Pow(2, 6) 23 | two2SixBig := new(big.Float).SetPrec(precision).SetFloat64(two2Six) 24 | 25 | for { 26 | if k > int(accuracy) { 27 | break 28 | } 29 | t1 := float64(float64(1) / float64(10*k+9)) 30 | k1 := new(big.Float).SetPrec(precision).SetFloat64(t1) 31 | t2 := float64(float64(64) / float64(10*k+3)) 32 | k2 := new(big.Float).SetPrec(precision).SetFloat64(t2) 33 | t3 := float64(float64(32) / float64(4*k+1)) 34 | k3 := new(big.Float).SetPrec(precision).SetFloat64(t3) 35 | k1k2k3.Sub(k1, k2) 36 | k1k2k3.Sub(k1k2k3, k3) 37 | 38 | t4 := float64(float64(4) / float64(10*k+5)) 39 | k4 := new(big.Float).SetPrec(precision).SetFloat64(t4) 40 | t5 := float64(float64(4) / float64(10*k+7)) 41 | k5 := new(big.Float).SetPrec(precision).SetFloat64(t5) 42 | t6 := float64(float64(1) / float64(4*k+3)) 43 | k6 := new(big.Float).SetPrec(precision).SetFloat64(t6) 44 | k4k5k6.Add(k4, k5) 45 | k4k5k6.Add(k4k5k6, k6) 46 | k4k5k6 = k4k5k6.Mul(k4k5k6, minusOne) 47 | temp.Add(k1k2k3, k4k5k6) 48 | 49 | k7temp := new(big.Int).Exp(big.NewInt(-1), big.NewInt(int64(k)), nil) 50 | k8temp := new(big.Int).Exp(big.NewInt(1024), big.NewInt(int64(k)), nil) 51 | 52 | k7 := new(big.Float).SetPrec(precision).SetFloat64(0) 53 | k7.SetInt(k7temp) 54 | k8 := new(big.Float).SetPrec(precision).SetFloat64(0) 55 | k8.SetInt(k8temp) 56 | 57 | t9 := float64(256) / float64(10*k+1) 58 | k9 := new(big.Float).SetPrec(precision).SetFloat64(t9) 59 | k9.Add(k9, temp) 60 | total.Mul(k9, k7) 61 | total.Quo(total, k8) 62 | pi.Add(pi, total) 63 | 64 | k = k + 1 65 | } 66 | pi.Quo(pi, two2SixBig) 67 | return pi 68 | } 69 | 70 | func main() { 71 | arguments := os.Args 72 | if len(arguments) == 1 { 73 | fmt.Println("Please provide one numeric argument!") 74 | os.Exit(1) 75 | } 76 | 77 | temp, _ := strconv.ParseUint(arguments[1], 10, 32) 78 | precision = uint(temp) * 3 79 | 80 | PI := Pi(precision) 81 | fmt.Println(PI) 82 | } 83 | 84 | -------------------------------------------------------------------------------- /day2/lab6/switch.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "regexp" 7 | "strconv" 8 | ) 9 | 10 | func main() { 11 | 12 | arguments := os.Args 13 | if len(arguments) < 2 { 14 | fmt.Println("usage: switch number.") 15 | os.Exit(1) 16 | } 17 | 18 | number, err := strconv.Atoi(arguments[1]) 19 | if err != nil { 20 | fmt.Println("This value is not an integer:", number) 21 | } else { 22 | switch { 23 | case number < 0: 24 | fmt.Println("Less than zero!") 25 | case number > 0: 26 | fmt.Println("Bigger than zero!") 27 | default: 28 | fmt.Println("Zero!") 29 | } 30 | } 31 | 32 | asString := arguments[1] 33 | switch asString { 34 | case "5": 35 | fmt.Println("Five!") 36 | case "0": 37 | fmt.Println("Zero!") 38 | default: 39 | fmt.Println("Do not care!") 40 | } 41 | 42 | var negative = regexp.MustCompile(`-`) 43 | var floatingPoint = regexp.MustCompile(`\d?\.\d`) 44 | var email = regexp.MustCompile(`^[^@]+@[^@.]+\.[^@.]+`) 45 | 46 | switch { 47 | case negative.MatchString(asString): 48 | fmt.Println("Negative number") 49 | case floatingPoint.MatchString(asString): 50 | fmt.Println("Floating point!") 51 | case email.MatchString(asString): 52 | fmt.Println("It is an email!") 53 | fallthrough 54 | default: 55 | fmt.Println("Something else!") 56 | } 57 | 58 | var aType error = nil 59 | switch aType.(type) { 60 | case nil: 61 | fmt.Println("It is nil interface!") 62 | default: 63 | fmt.Println("Not nil interface!") 64 | } 65 | } 66 | 67 | -------------------------------------------------------------------------------- /day2/lab6/usingTime.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func main() { 9 | fmt.Println("Epoch time:", time.Now().Unix()) 10 | t := time.Now() 11 | fmt.Println(t, t.Format(time.RFC3339)) 12 | fmt.Println(t.Weekday(), t.Day(), t.Month(), t.Year()) 13 | 14 | time.Sleep(time.Second) 15 | t1 := time.Now() 16 | fmt.Println("Time difference:", t1.Sub(t)) 17 | 18 | formatT := t.Format("01 January 2006") 19 | fmt.Println(formatT) 20 | loc, _ := time.LoadLocation("Europe/Paris") 21 | londonTime := t.In(loc) 22 | fmt.Println("Paris:", londonTime) 23 | } 24 | 25 | -------------------------------------------------------------------------------- /day2/lab6/webClient.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "net/http" 7 | "os" 8 | "path/filepath" 9 | ) 10 | 11 | func main() { 12 | if len(os.Args) != 2 { 13 | fmt.Printf("Usage: %s URL\n", filepath.Base(os.Args[0])) 14 | return 15 | } 16 | 17 | URL := os.Args[1] 18 | 19 | data, err := http.Get(URL) 20 | 21 | if err != nil { 22 | fmt.Println(err) 23 | return 24 | } else { 25 | defer data.Body.Close() 26 | _, err := io.Copy(os.Stdout, data.Body) 27 | if err != nil { 28 | fmt.Println(err) 29 | return 30 | } 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /day2/lab6/www.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "os" 7 | "time" 8 | ) 9 | 10 | func myHandler(w http.ResponseWriter, r *http.Request) { 11 | fmt.Fprintf(w, "Serving: %s\n", r.URL.Path) 12 | fmt.Printf("Served: %s\n", r.Host) 13 | } 14 | 15 | func timeHandler(w http.ResponseWriter, r *http.Request) { 16 | t := time.Now().Format(time.RFC1123) 17 | Body := "The current time is:" 18 | fmt.Fprintf(w, "

%s

", Body) 19 | fmt.Fprintf(w, "

%s

\n", t) 20 | fmt.Fprintf(w, "Serving: %s\n", r.URL.Path) 21 | fmt.Printf("Served time for: %s\n", r.Host) 22 | } 23 | 24 | func testHandler(w http.ResponseWriter, r *http.Request) { 25 | fmt.Fprintf(w, "Serving: %s + /test \n", r.URL.Path) 26 | fmt.Printf("Served /test for: %s\n", r.Host) 27 | } 28 | 29 | func main() { 30 | PORT := ":8001" 31 | arguments := os.Args 32 | if len(arguments) != 1 { 33 | PORT = ":" + arguments[1] 34 | } 35 | fmt.Println("Using port number: ", PORT) 36 | 37 | http.HandleFunc("/time", timeHandler) 38 | http.HandleFunc("/test", testHandler) 39 | http.HandleFunc("/", myHandler) 40 | 41 | err := http.ListenAndServe(PORT, nil) 42 | if err != nil { 43 | fmt.Println(err) 44 | return 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /day2/s4/create10.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | count := 10 9 | fmt.Printf("Going to create %d goroutines.\n", count) 10 | 11 | for i := 0; i < count; i++ { 12 | go func(x int) { 13 | fmt.Printf("%d ", x) 14 | }(i) 15 | } 16 | 17 | fmt.Println("\nExiting...") 18 | } 19 | 20 | -------------------------------------------------------------------------------- /day2/s4/create10sleep.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func main() { 9 | count := 10 10 | fmt.Printf("Going to create %d goroutines.\n", count) 11 | 12 | for i := 0; i < count; i++ { 13 | go func(x int) { 14 | fmt.Printf("%d ", x) 15 | }(i) 16 | } 17 | 18 | time.Sleep(time.Second) 19 | fmt.Println("\nExiting...") 20 | } 21 | 22 | -------------------------------------------------------------------------------- /day2/s4/pipelines.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "path/filepath" 7 | "strconv" 8 | ) 9 | 10 | func genNumbers(min, max int64, out chan<- int64) { 11 | var i int64 12 | for i = min; i <= max; i++ { 13 | out <- i 14 | } 15 | close(out) 16 | } 17 | 18 | func findSquares(out chan<- int64, in <-chan int64) { 19 | for x := range in { 20 | out <- x * x 21 | } 22 | close(out) 23 | } 24 | 25 | func calcSum(in <-chan int64) { 26 | var sum int64 27 | sum = 0 28 | for x2 := range in { 29 | sum = sum + x2 30 | } 31 | fmt.Printf("The sum of squares is %d\n", sum) 32 | } 33 | 34 | func main() { 35 | if len(os.Args) != 3 { 36 | fmt.Printf("usage: %s n1 n2\n", filepath.Base(os.Args[0])) 37 | os.Exit(1) 38 | } 39 | n1, _ := strconv.ParseInt(os.Args[1], 10, 64) 40 | n2, _ := strconv.ParseInt(os.Args[2], 10, 64) 41 | 42 | if n1 > n2 { 43 | fmt.Printf("%d should be smaller than %d\n", n1, n2) 44 | os.Exit(10) 45 | } 46 | 47 | naturals := make(chan int64) 48 | squares := make(chan int64) 49 | go genNumbers(n1, n2, naturals) 50 | go findSquares(squares, naturals) 51 | calcSum(squares) 52 | } 53 | 54 | -------------------------------------------------------------------------------- /day2/s4/readWriteCh.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func writeChannel(c chan<- int, x int) { 9 | fmt.Println(x) 10 | c <- x 11 | time.Sleep(3 * time.Second) 12 | fmt.Println(x) 13 | close(c) 14 | } 15 | 16 | func main() { 17 | c := make(chan int) 18 | 19 | go writeChannel(c, 10) 20 | // fmt.Println("Read:", <-c) 21 | time.Sleep(2 * time.Second) 22 | 23 | value, ok := <-c 24 | if ok { 25 | fmt.Println("Channel is open!") 26 | fmt.Println("Value: ", value) 27 | } else { 28 | fmt.Println("Channel is closed!") 29 | } 30 | 31 | time.Sleep(4 * time.Second) 32 | _, ok = <-c 33 | if ok { 34 | fmt.Println("Channel is open!") 35 | } else { 36 | fmt.Println("Channel is closed!") 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /day2/s4/usingSync.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sync" 6 | ) 7 | 8 | func main() { 9 | count := 10 10 | fmt.Printf("Going to create %d goroutines.\n", count) 11 | 12 | var waitGroup sync.WaitGroup 13 | fmt.Printf("%#v\n", waitGroup) 14 | 15 | for i := 0; i < count; i++ { 16 | waitGroup.Add(1) 17 | go func(x int) { 18 | defer waitGroup.Done() 19 | fmt.Printf("%d ", x) 20 | }(i) 21 | } 22 | 23 | fmt.Printf("%#v\n", waitGroup) 24 | waitGroup.Wait() 25 | fmt.Println("\nExiting...") 26 | } 27 | 28 | -------------------------------------------------------------------------------- /day2/s4/writeCh.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func writeChannel(c chan int, x int) { 9 | fmt.Println(x) 10 | c <- x 11 | temp, _ := <- c 12 | fmt.Println(temp) 13 | close(c) 14 | fmt.Println(x) 15 | } 16 | 17 | func main() { 18 | c := make(chan int) 19 | go writeChannel(c, 10) 20 | time.Sleep(2 * time.Second) 21 | } 22 | 23 | -------------------------------------------------------------------------------- /day2/s5/concTCP.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "math/rand" 7 | "net" 8 | "os" 9 | "strconv" 10 | "strings" 11 | "time" 12 | ) 13 | 14 | const MIN = 1 15 | const MAX = 100 16 | 17 | func random() int { 18 | return rand.Intn(MAX-MIN) + MIN 19 | } 20 | 21 | func handleConnection(c net.Conn) { 22 | fmt.Printf("Serving %s\n", c.RemoteAddr().String()) 23 | for { 24 | netData, err := bufio.NewReader(c).ReadString('\n') 25 | if err != nil { 26 | fmt.Println(err) 27 | return 28 | } 29 | 30 | temp := strings.TrimSpace(string(netData)) 31 | if temp == "STOP" { 32 | break 33 | } 34 | 35 | result := strconv.Itoa(random()) + "\n" 36 | c.Write([]byte(string(result))) 37 | } 38 | c.Close() 39 | } 40 | 41 | func main() { 42 | arguments := os.Args 43 | if len(arguments) == 1 { 44 | fmt.Println("Please provide a port number!") 45 | return 46 | } 47 | 48 | PORT := ":" + arguments[1] 49 | l, err := net.Listen("tcp4", PORT) 50 | if err != nil { 51 | fmt.Println(err) 52 | return 53 | } 54 | defer l.Close() 55 | rand.Seed(time.Now().Unix()) 56 | 57 | for { 58 | c, err := l.Accept() 59 | if err != nil { 60 | fmt.Println(err) 61 | return 62 | } 63 | go handleConnection(c) 64 | } 65 | } 66 | 67 | -------------------------------------------------------------------------------- /day2/s5/tC.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "net" 7 | "os" 8 | "strings" 9 | ) 10 | 11 | func main() { 12 | arguments := os.Args 13 | if len(arguments) == 1 { 14 | fmt.Println("Please provide host:port.") 15 | return 16 | } 17 | 18 | CONNECT := arguments[1] 19 | c, err := net.Dial("tcp", CONNECT) 20 | 21 | if err != nil { 22 | fmt.Println(err) 23 | return 24 | } 25 | 26 | for { 27 | reader := bufio.NewReader(os.Stdin) 28 | fmt.Print(">> ") 29 | text, _ := reader.ReadString('\n') 30 | fmt.Fprintf(c, text+"\n") 31 | 32 | message, _ := bufio.NewReader(c).ReadString('\n') 33 | fmt.Print("->: " + message) 34 | if strings.TrimSpace(string(text)) == "STOP" { 35 | fmt.Println("client exiting...") 36 | return 37 | } 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /day2/s5/tS.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "net" 7 | "os" 8 | "strings" 9 | "time" 10 | ) 11 | 12 | func main() { 13 | arguments := os.Args 14 | if len(arguments) == 1 { 15 | fmt.Println("Please provide port number") 16 | return 17 | } 18 | 19 | PORT := ":" + arguments[1] 20 | l, err := net.Listen("tcp4", PORT) 21 | if err != nil { 22 | fmt.Println(err) 23 | return 24 | } 25 | defer l.Close() 26 | 27 | c, err := l.Accept() 28 | if err != nil { 29 | fmt.Println(err) 30 | return 31 | } 32 | 33 | for { 34 | netData, err := bufio.NewReader(c).ReadString('\n') 35 | if err != nil { 36 | fmt.Println(err) 37 | return 38 | } 39 | if strings.TrimSpace(string(netData)) == "STOP" { 40 | fmt.Println("TCP server!") 41 | return 42 | } 43 | 44 | fmt.Print("-> ", string(netData)) 45 | t := time.Now() 46 | myTime := t.Format(time.RFC3339) + "\n" 47 | c.Write([]byte(myTime)) 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /day2/s5/uC.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "net" 7 | "os" 8 | "strings" 9 | ) 10 | 11 | func main() { 12 | arguments := os.Args 13 | if len(arguments) == 1 { 14 | fmt.Println("Please provide a host:port string") 15 | return 16 | } 17 | 18 | CONNECT := arguments[1] 19 | s, err := net.ResolveUDPAddr("udp4", CONNECT) 20 | c, err := net.DialUDP("udp4", nil, s) 21 | if err != nil { 22 | fmt.Println(err) 23 | return 24 | } 25 | 26 | fmt.Printf("The UDP server is %s\n", c.RemoteAddr().String()) 27 | defer c.Close() 28 | 29 | for { 30 | reader := bufio.NewReader(os.Stdin) 31 | fmt.Print(">> ") 32 | text, _ := reader.ReadString('\n') 33 | data := []byte(text + "\n") 34 | _, err = c.Write(data) 35 | if strings.TrimSpace(string(data)) == "STOP" { 36 | fmt.Println("Exiting UDP client!") 37 | return 38 | } 39 | if err != nil { 40 | fmt.Println(err) 41 | return 42 | } 43 | buffer := make([]byte, 1024) 44 | n, _, err := c.ReadFromUDP(buffer) 45 | if err != nil { 46 | fmt.Println(err) 47 | return 48 | } 49 | fmt.Printf("Reply: %s\n", string(buffer[0:n])) 50 | } 51 | } 52 | 53 | -------------------------------------------------------------------------------- /day2/s5/uS.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "net" 7 | "os" 8 | "strconv" 9 | "strings" 10 | "time" 11 | ) 12 | 13 | func random(min, max int) int { 14 | return rand.Intn(max-min) + min 15 | } 16 | 17 | func main() { 18 | arguments := os.Args 19 | if len(arguments) == 1 { 20 | fmt.Println("Provide a port number!") 21 | return 22 | } 23 | 24 | PORT := ":" + arguments[1] 25 | 26 | s, err := net.ResolveUDPAddr("udp4", PORT) 27 | if err != nil { 28 | fmt.Println(err) 29 | return 30 | } 31 | 32 | connection, err := net.ListenUDP("udp4", s) 33 | if err != nil { 34 | fmt.Println(err) 35 | return 36 | } 37 | defer connection.Close() 38 | 39 | buffer := make([]byte, 1024) 40 | rand.Seed(time.Now().Unix()) 41 | for { 42 | n, addr, err := connection.ReadFromUDP(buffer) 43 | fmt.Print("-> ", string(buffer[0:n-1])) 44 | 45 | if strings.TrimSpace(string(buffer[0:n])) == "STOP" { 46 | fmt.Println("Exiting UDP server!") 47 | return 48 | } 49 | 50 | data := []byte(strconv.Itoa(random(1, 1001))) 51 | fmt.Printf("data: %s\n", string(data)) 52 | _, err = connection.WriteToUDP(data, addr) 53 | if err != nil { 54 | fmt.Println(err) 55 | return 56 | } 57 | } 58 | } 59 | 60 | -------------------------------------------------------------------------------- /day2/s6/httpClient.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "net/http" 7 | "os" 8 | "path/filepath" 9 | ) 10 | 11 | func main() { 12 | if len(os.Args) != 2 { 13 | fmt.Printf("Usage: %s URL\n", filepath.Base(os.Args[0])) 14 | return 15 | } 16 | 17 | URL := os.Args[1] 18 | data, err := http.Get(URL) 19 | 20 | if err != nil { 21 | fmt.Println(err) 22 | return 23 | } else { 24 | defer data.Body.Close() 25 | _, err := io.Copy(os.Stdout, data.Body) 26 | if err != nil { 27 | fmt.Println(err) 28 | return 29 | } 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /day2/s6/www.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "os" 7 | "time" 8 | ) 9 | 10 | func myHandler(w http.ResponseWriter, r *http.Request) { 11 | fmt.Fprintf(w, "Serving: %s\n", r.URL.Path) 12 | fmt.Printf("Served: %s\n", r.Host) 13 | } 14 | 15 | func timeHandler(w http.ResponseWriter, r *http.Request) { 16 | t := time.Now().Format(time.RFC1123) 17 | Body := "The current time is:" 18 | fmt.Fprintf(w, "

%s

", Body) 19 | fmt.Fprintf(w, "

%s

\n", t) 20 | fmt.Fprintf(w, "Serving: %s\n", r.URL.Path) 21 | fmt.Printf("Served /time for: %s\n", r.Host) 22 | } 23 | 24 | func main() { 25 | PORT := ":8001" 26 | arguments := os.Args 27 | if len(arguments) != 1 { 28 | PORT = ":" + arguments[1] 29 | } 30 | fmt.Println("Using port number: ", PORT) 31 | 32 | http.HandleFunc("/time", timeHandler) 33 | http.HandleFunc("/date", timeHandler) 34 | http.HandleFunc("/", myHandler) 35 | 36 | err := http.ListenAndServe(PORT, nil) 37 | if err != nil { 38 | fmt.Println(err) 39 | return 40 | } 41 | } 42 | 43 | --------------------------------------------------------------------------------