diff --git a/roihan/flowcontrol_14.go b/roihan/flowcontrol_14.go index 26fe8ba..66d7c84 100644 --- a/roihan/flowcontrol_14.go +++ b/roihan/flowcontrol_14.go @@ -4,4 +4,5 @@ import "fmt" func flowcontrol14() { fmt.Println("You finished this lesson!") + fmt.Println() } diff --git a/roihan/moretypes_1.go b/roihan/moretypes_1.go new file mode 100644 index 0000000..4a50ae3 --- /dev/null +++ b/roihan/moretypes_1.go @@ -0,0 +1,16 @@ +package roihan + +import "fmt" + +func moretype1() { + i, j := 42, 2701 + + p := &i + fmt.Println(*p) + *p = 21 + fmt.Println(i) + + p = &j + *p = *p / 37 + fmt.Println(j) +} diff --git a/roihan/moretypes_10.go b/roihan/moretypes_10.go new file mode 100644 index 0000000..c714761 --- /dev/null +++ b/roihan/moretypes_10.go @@ -0,0 +1,16 @@ +package roihan + +import "fmt" + +func moretype10() { + 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/roihan/moretypes_11.go b/roihan/moretypes_11.go new file mode 100644 index 0000000..18fb89f --- /dev/null +++ b/roihan/moretypes_11.go @@ -0,0 +1,21 @@ +package roihan + +import "fmt" + +func moretype11() { + s := []int{2, 3, 5, 7, 11, 13} + printSlice(s) + + s = s[:0] + printSlice(s) + + s = s[:4] + printSlice(s) + + 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/roihan/moretypes_12.go b/roihan/moretypes_12.go new file mode 100644 index 0000000..0b7c27b --- /dev/null +++ b/roihan/moretypes_12.go @@ -0,0 +1,11 @@ +package roihan + +import "fmt" + +func moretype12() { + var s []int + fmt.Println(s, len(s), cap(s)) + if s == nil { + fmt.Println("nil!") + } +} diff --git a/roihan/moretypes_13.go b/roihan/moretypes_13.go new file mode 100644 index 0000000..d2bf90b --- /dev/null +++ b/roihan/moretypes_13.go @@ -0,0 +1,21 @@ +package roihan + +import "fmt" + +func moretype13() { + 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/roihan/moretypes_14.go b/roihan/moretypes_14.go new file mode 100644 index 0000000..a9e1d03 --- /dev/null +++ b/roihan/moretypes_14.go @@ -0,0 +1,24 @@ +package roihan + +import ( + "fmt" + "strings" +) + +func moretype14() { + board := [][]string{ + []string{"_", "_", "_"}, + []string{"_", "_", "_"}, + []string{"_", "_", "_"}, + } + + 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/roihan/moretypes_15.go b/roihan/moretypes_15.go new file mode 100644 index 0000000..8bc67bd --- /dev/null +++ b/roihan/moretypes_15.go @@ -0,0 +1,21 @@ +package roihan + +import "fmt" + +func moretype15() { + var s []int + Printslice(s) + + s = append(s, 0) + Printslice(s) + + s = append(s, 1) + Printslice(s) + + 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/roihan/moretypes_16.go b/roihan/moretypes_16.go new file mode 100644 index 0000000..3f376a6 --- /dev/null +++ b/roihan/moretypes_16.go @@ -0,0 +1,11 @@ +package roihan + +import "fmt" + +var pos = []int{1, 2, 4, 8, 16, 32, 64, 128} + +func moretype16() { + for i, v := range pos { + fmt.Printf("2**%d = %d\n", i, v) + } +} diff --git a/roihan/moretypes_17.go b/roihan/moretypes_17.go new file mode 100644 index 0000000..5a35548 --- /dev/null +++ b/roihan/moretypes_17.go @@ -0,0 +1,13 @@ +package roihan + +import "fmt" + +func moretype17() { + por := make([]int, 10) + for i := range por { + por[i] = 1 << uint(i) + } + for _, value := range por { + fmt.Printf("%d\n", value) + } +} diff --git a/roihan/moretypes_19.go b/roihan/moretypes_19.go new file mode 100644 index 0000000..26e1f70 --- /dev/null +++ b/roihan/moretypes_19.go @@ -0,0 +1,17 @@ +package roihan + +import "fmt" + +type vertex struct { + Lat, Long float64 +} + +var m map[string]vertex + +func moretype19() { + m = make(map[string]vertex) + m["Bell Labs"] = vertex{ + 40.648433, -74.39967, + } + fmt.Println(m["Bell Labs"]) +} diff --git a/roihan/moretypes_2.go b/roihan/moretypes_2.go new file mode 100644 index 0000000..48166bb --- /dev/null +++ b/roihan/moretypes_2.go @@ -0,0 +1,12 @@ +package roihan + +import "fmt" + +type Vertex struct { + X int + Y int +} + +func moretype2() { + fmt.Println(Vertex{1, 20}) +} diff --git a/roihan/moretypes_20.go b/roihan/moretypes_20.go new file mode 100644 index 0000000..7c9204e --- /dev/null +++ b/roihan/moretypes_20.go @@ -0,0 +1,20 @@ +package roihan + +import "fmt" + +type Verte struct { + Lat, Long float64 +} + +var ma = map[string]Verte{ + "Bell Labs": Verte{ + 40.648433, -74.39967, + }, + "Google": Verte{ + 37.42202, -122.08408, + }, +} + +func moretype20() { + fmt.Println(ma) +} diff --git a/roihan/moretypes_21.go b/roihan/moretypes_21.go new file mode 100644 index 0000000..d286f0d --- /dev/null +++ b/roihan/moretypes_21.go @@ -0,0 +1,16 @@ +package roihan + +import "fmt" + +type literals struct { + Lat, Long float64 +} + +var mp = map[string]literals{ + "Bell Labs": {40.68433, -74.39967}, + "Google": {37.42202, -122.08408}, +} + +func moretype21() { + fmt.Println(mp) +} diff --git a/roihan/moretypes_22.go b/roihan/moretypes_22.go new file mode 100644 index 0000000..7ec750b --- /dev/null +++ b/roihan/moretypes_22.go @@ -0,0 +1,19 @@ +package roihan + +import "fmt" + +func moretype22() { + 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/roihan/moretypes_23.go b/roihan/moretypes_23.go new file mode 100644 index 0000000..7619c66 --- /dev/null +++ b/roihan/moretypes_23.go @@ -0,0 +1,11 @@ +package roihan + +import "golang.org/x/tour/wc" + +func WordCount(s string) map[string]int { + return map[string]int{"x": 1} +} + +func moretype23() { + wc.Test(WordCount) +} diff --git a/roihan/moretypes_24.go b/roihan/moretypes_24.go new file mode 100644 index 0000000..8d40496 --- /dev/null +++ b/roihan/moretypes_24.go @@ -0,0 +1,20 @@ +package roihan + +import ( + "fmt" + "math" +) + +func compute(fn func(float64, float64) float64) float64 { + return fn(3, 4) +} + +func moretype24() { + 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/roihan/moretypes_3.go b/roihan/moretypes_3.go new file mode 100644 index 0000000..ba13d49 --- /dev/null +++ b/roihan/moretypes_3.go @@ -0,0 +1,14 @@ +package roihan + +import "fmt" + +type moretype struct { + X int + Y int +} + +func moretype3() { + v := Vertex{1, 2} + v.X = 4 + fmt.Println(v.X) +} diff --git a/roihan/moretypes_4.go b/roihan/moretypes_4.go new file mode 100644 index 0000000..543bb96 --- /dev/null +++ b/roihan/moretypes_4.go @@ -0,0 +1,15 @@ +package roihan + +import "fmt" + +type Moretype struct { + X int + Y int +} + +func moretype4() { + v := Moretype{1, 2} + p := &v + p.X = 1e9 + fmt.Println(v) +} diff --git a/roihan/moretypes_5.go b/roihan/moretypes_5.go new file mode 100644 index 0000000..53feb48 --- /dev/null +++ b/roihan/moretypes_5.go @@ -0,0 +1,18 @@ +package roihan + +import "fmt" + +type MoretypeVertex struct { + X, Y int +} + +var ( + v1 = MoretypeVertex{1, 2} //Has type vertex + v2 = MoretypeVertex{X: 1} //Y:0 is implicit + v3 = MoretypeVertex{} //X:0 and Y:0 + p = &MoretypeVertex{1, 2} //Has type *Vertex +) + +func moretype5() { + fmt.Println(v1, p, v2, v3) +} diff --git a/roihan/moretypes_6.go b/roihan/moretypes_6.go new file mode 100644 index 0000000..cbf3bd3 --- /dev/null +++ b/roihan/moretypes_6.go @@ -0,0 +1,14 @@ +package roihan + +import "fmt" + +func moretype6() { + 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, 11, 13} + fmt.Println(primes) +} diff --git a/roihan/moretypes_7.go b/roihan/moretypes_7.go new file mode 100644 index 0000000..7a7a121 --- /dev/null +++ b/roihan/moretypes_7.go @@ -0,0 +1,10 @@ +package roihan + +import "fmt" + +func moretype7() { + primes := [6]int{2, 3, 5, 7, 11, 13} + + var s []int = primes[1:4] + fmt.Println(s) +} diff --git a/roihan/moretypes_8.go b/roihan/moretypes_8.go new file mode 100644 index 0000000..204137c --- /dev/null +++ b/roihan/moretypes_8.go @@ -0,0 +1,21 @@ +package roihan + +import "fmt" + +func moretype8() { + names := [3]string{ + "Roihan", + "Mishbahul", + "Anam", + } + + fmt.Println(names) + + a := names[0:2] + b := names[1:3] + fmt.Println(a, b) + + b[0] = "XXX" + fmt.Println(a, b) + fmt.Println(names) +} diff --git a/roihan/moretypes_9.go b/roihan/moretypes_9.go new file mode 100644 index 0000000..cbbb044 --- /dev/null +++ b/roihan/moretypes_9.go @@ -0,0 +1,24 @@ +package roihan + +import "fmt" + +func moretype9() { + q := []int{2, 3, 5, 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/roihan/roihan.go b/roihan/roihan.go index 4e16e09..c328e5c 100644 --- a/roihan/roihan.go +++ b/roihan/roihan.go @@ -40,6 +40,32 @@ func Main() { flowcontrol12() flowcontrol13() flowcontrol14() + + //Moretypes + fmt.Println("//Moretypes//") + moretype1() + moretype2() + moretype3() + moretype4() + moretype5() + moretype6() + moretype7() + moretype8() + moretype9() + moretype10() + moretype11() + moretype12() + moretype13() + moretype14() + moretype15() + moretype16() + moretype17() + moretype19() + moretype20() + moretype21() + moretype22() + moretype23() + moretype24() } func basics1() {