diff --git a/sidik/moretypes_1.go b/sidik/moretypes_1.go new file mode 100644 index 0000000..d48febc --- /dev/null +++ b/sidik/moretypes_1.go @@ -0,0 +1,14 @@ +package sidik + +import "fmt" + +func type1() { + i := 1000 + p := &i + fmt.Println("i before = ",i) + + *p = 2000 + + fmt.Println("i after = ",i) + +} \ No newline at end of file diff --git a/sidik/moretypes_10.go b/sidik/moretypes_10.go new file mode 100644 index 0000000..df60b5c --- /dev/null +++ b/sidik/moretypes_10.go @@ -0,0 +1,17 @@ +package sidik + +import "fmt" + +func type10() { + s := []int{4,5,3,1,3,8,9} + + s = s[1:4] + fmt.Println(s) + + s = s[:2] + fmt.Println(s) + + s = s[:1] + fmt.Println(s) + +} \ No newline at end of file diff --git a/sidik/moretypes_11.go b/sidik/moretypes_11.go new file mode 100644 index 0000000..08c2082 --- /dev/null +++ b/sidik/moretypes_11.go @@ -0,0 +1,21 @@ +package sidik + +import "fmt" + +func type11() { + s := []int{5, 2, 6, 7, 8, 9, 10} + 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) +} \ No newline at end of file diff --git a/sidik/moretypes_12.go b/sidik/moretypes_12.go new file mode 100644 index 0000000..6f725b4 --- /dev/null +++ b/sidik/moretypes_12.go @@ -0,0 +1,12 @@ +package sidik + +import "fmt" + +func type12() { + var s []int + fmt.Println(s, len(s), cap(s)) + if s == nil { + fmt.Println("nil!") + } + +} \ No newline at end of file diff --git a/sidik/moretypes_13.go b/sidik/moretypes_13.go new file mode 100644 index 0000000..2751feb --- /dev/null +++ b/sidik/moretypes_13.go @@ -0,0 +1,21 @@ +package sidik + +import "fmt" + +func type13() { + a := make([]int , 5) + printSlice2("a", a) + + b := make([]int, 0, 5) + printSlice2("b", b) + + c := b[:2] + printSlice2("c", c) + + d := c[2:5] + printSlice2("d", d) +} + +func printSlice2(s string ,x []int) { + fmt.Printf("%s len=%d cap=%d %v\n", s , len(x), cap(x), x) +} \ No newline at end of file diff --git a/sidik/moretypes_14.go b/sidik/moretypes_14.go new file mode 100644 index 0000000..e61d186 --- /dev/null +++ b/sidik/moretypes_14.go @@ -0,0 +1,24 @@ +package sidik + +import ( + "fmt" + "strings" +) + +func type14() { + 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], "")) + } +} \ No newline at end of file diff --git a/sidik/moretypes_15.go b/sidik/moretypes_15.go new file mode 100644 index 0000000..c05a7ce --- /dev/null +++ b/sidik/moretypes_15.go @@ -0,0 +1,19 @@ +package sidik + +import "fmt" + +func type15() { + var s []int + printSlice3(s) + + s = append(s, 1) + printSlice3(s) + + s = append(s, 2,3,4) + printSlice3(s) + +} + +func printSlice3(s []int) { + fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s) +} \ No newline at end of file diff --git a/sidik/moretypes_16.go b/sidik/moretypes_16.go new file mode 100644 index 0000000..a90ce60 --- /dev/null +++ b/sidik/moretypes_16.go @@ -0,0 +1,11 @@ +package sidik + +import "fmt" + +var poq = []int{1, 2, 4, 8, 16, 32, 64, 128} + +func type16() { + for i, v := range poq { + fmt.Printf("2**%d = %d\n", i, v) + } +} \ No newline at end of file diff --git a/sidik/moretypes_17.go b/sidik/moretypes_17.go new file mode 100644 index 0000000..104d359 --- /dev/null +++ b/sidik/moretypes_17.go @@ -0,0 +1,15 @@ +package sidik + +import "fmt" + +func type17() { + ppow := make([]int, 10) + for i := range ppow { + ppow[i] = 1 << uint(i) // 2**i + + } + + for _, value := range ppow { + fmt.Printf("%d\n", value) + } +} \ No newline at end of file diff --git a/sidik/moretypes_18.go b/sidik/moretypes_18.go new file mode 100644 index 0000000..7a00622 --- /dev/null +++ b/sidik/moretypes_18.go @@ -0,0 +1,21 @@ +package sidik + +import ( + "golang.org/x/tour/pic" +) + +func Pic(dx, dy int) [][]uint8 { + sdx := make([][]uint8, dx) + for y := 0; y < dy; y++ { + sdy := make([]uint8, dy) + for x := 0; x < dx; x++ { + sdy[x] = uint8((x+y)*4) + } + sdx[y] = sdy + } + return sdx +} + +func type18() { + pic.Show(Pic) +} \ No newline at end of file diff --git a/sidik/moretypes_19.go b/sidik/moretypes_19.go new file mode 100644 index 0000000..22cfc81 --- /dev/null +++ b/sidik/moretypes_19.go @@ -0,0 +1,19 @@ +package sidik + +import "fmt" + +type Vertexx struct { + Lat, Long float64 +} + +var m map[string]Vertexx + +func type19() { + m = make(map[string]Vertexx) + m["Bell Labs"] = Vertexx{ + 40.68433, -74.39967, + } + + fmt.Println(m["Bell Labs"]) + +} \ No newline at end of file diff --git a/sidik/moretypes_2.go b/sidik/moretypes_2.go new file mode 100644 index 0000000..f4b1a8c --- /dev/null +++ b/sidik/moretypes_2.go @@ -0,0 +1,13 @@ +package sidik + +import "fmt" + +type Vertex struct { + X int + Y int + Z int +} + +func type2() { + fmt.Println(Vertex{7, 5, 8}) +} \ No newline at end of file diff --git a/sidik/moretypes_20.go b/sidik/moretypes_20.go new file mode 100644 index 0000000..d4ed66f --- /dev/null +++ b/sidik/moretypes_20.go @@ -0,0 +1,20 @@ +package sidik + +import "fmt" + +type Vertex4 struct{ + Lat, Long float64 +} + +var mn = map[string]Vertex4{ + "Bell Labs" : Vertex4{ + 40.68433, -74.39967, + }, + "Google Inc" : Vertex4{ + 37.42202, -122.08408, + }, +} + +func type20() { + fmt.Println(mn) +} \ No newline at end of file diff --git a/sidik/moretypes_21.go b/sidik/moretypes_21.go new file mode 100644 index 0000000..553680f --- /dev/null +++ b/sidik/moretypes_21.go @@ -0,0 +1,16 @@ +package sidik + +import "fmt" + +type Vertex5 struct { + Lat, Long float64 +} + +var maa = map[string]Vertex5{ + "Bell Labs": {40.68433, -74.39967}, + "Google Inc" : {37.42202, -122.08408}, +} + +func type21() { + fmt.Println(maa) +} \ No newline at end of file diff --git a/sidik/moretypes_22.go b/sidik/moretypes_22.go new file mode 100644 index 0000000..e4f8acf --- /dev/null +++ b/sidik/moretypes_22.go @@ -0,0 +1,19 @@ +package sidik + +import "fmt" + +func type22() { + mm := make(map[string]int) + + mm["Answer"] = 42 + fmt.Println("The Value: ", mm["Answer"]) + + mm["Answer"] = 48 + fmt.Println("The Value: ", mm["Answer"]) + + delete(mm, "Answer") + fmt.Println("The Value: ", mm["Answer"]) + + v, ok := mm["Answer"] + fmt.Println("The Value: ", v ,"Present", ok) +} \ No newline at end of file diff --git a/sidik/moretypes_23.go b/sidik/moretypes_23.go new file mode 100644 index 0000000..b7b76b3 --- /dev/null +++ b/sidik/moretypes_23.go @@ -0,0 +1,22 @@ +package sidik + +import ( + "strings" + + "golang.org/x/tour/wc" +) + +func WordCount(s string) map[string]int { + words := strings.Fields(s) + result := make(map[string]int) + + for _, word := range words{ + result[word] += 1 + } + + return result +} + +func type23() { + wc.Test(WordCount) +} diff --git a/sidik/moretypes_24.go b/sidik/moretypes_24.go new file mode 100644 index 0000000..9b60c2b --- /dev/null +++ b/sidik/moretypes_24.go @@ -0,0 +1,21 @@ +package sidik + +import ( + "fmt" + "math" +) + +func compute(fn func(float64, float64) float64) float64 { + return fn(3, 4) +} + +func type24() { + 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)) +} \ No newline at end of file diff --git a/sidik/moretypes_3.go b/sidik/moretypes_3.go new file mode 100644 index 0000000..6fe77bc --- /dev/null +++ b/sidik/moretypes_3.go @@ -0,0 +1,18 @@ +package sidik + +import "fmt" + +type Vertex1 struct { + X int + Y int + Z int +} + +func type3() { //mengakses properti struct dengan titik + p := Vertex{8, 4, 10} + p.X = 19 + p.Z = 8 + + fmt.Println(p.X, p.Z) + +} \ No newline at end of file diff --git a/sidik/moretypes_4.go b/sidik/moretypes_4.go new file mode 100644 index 0000000..806a94b --- /dev/null +++ b/sidik/moretypes_4.go @@ -0,0 +1,17 @@ +package sidik + +import "fmt" + +type Vertex2 struct { + X int + Y int +} + +func type4() { + i := Vertex2{2, 3} + p := &i + p.Y = 8 + + fmt.Println(i) + +} \ No newline at end of file diff --git a/sidik/moretypes_5.go b/sidik/moretypes_5.go new file mode 100644 index 0000000..0988015 --- /dev/null +++ b/sidik/moretypes_5.go @@ -0,0 +1,18 @@ +package sidik + +import "fmt" + +type Vertex3 struct { + X, Y int +} + +var ( + v = Vertex3{5, 44} // struct asli + v2 = Vertex3{X:6} // mengakses satu property di struct + v3 = Vertex3{} // struct zzero value + p = &Vertex3{3,4} //pointer struct, harus dengan * jika ingin value yang asli +) + +func type5() { + fmt.Println(v, v2, v3, p) +} \ No newline at end of file diff --git a/sidik/moretypes_6.go b/sidik/moretypes_6.go new file mode 100644 index 0000000..e481a04 --- /dev/null +++ b/sidik/moretypes_6.go @@ -0,0 +1,17 @@ +package sidik + +import "fmt" + +func type6() { + var s [3]string + s[0] = "sidik" + s[1] = "sudah" + s[2] = "go" + + fmt.Println(s[0], s[2]) + fmt.Println(s) + + primes := [4]int{1,2,3,4} + fmt.Println(primes) + +} \ No newline at end of file diff --git a/sidik/moretypes_7.go b/sidik/moretypes_7.go new file mode 100644 index 0000000..34b7c99 --- /dev/null +++ b/sidik/moretypes_7.go @@ -0,0 +1,10 @@ +package sidik + +import "fmt" + +func type7() { + prime := [6]int{12,3,42,4,5,3} + + var s []int = prime[1:4] + fmt.Println(s) +} \ No newline at end of file diff --git a/sidik/moretypes_8.go b/sidik/moretypes_8.go new file mode 100644 index 0000000..d41b740 --- /dev/null +++ b/sidik/moretypes_8.go @@ -0,0 +1,24 @@ +package sidik + +import "fmt" + +func type8() { + names := [4]string{ + "John", + "Paul", + "George", + "Ringo", + } + + fmt.Println(names) + + b := names[1:3] + a := names[0:2] + + fmt.Println(b, a) + + b[0] = "XXXx" + fmt.Println(b, a) + fmt.Println(names) + +} \ No newline at end of file diff --git a/sidik/moretypes_9.go b/sidik/moretypes_9.go new file mode 100644 index 0000000..58d8ef4 --- /dev/null +++ b/sidik/moretypes_9.go @@ -0,0 +1,29 @@ +package sidik + +import "fmt" + +func type9() { + v := []int{4, 5, 6, 33, 5, 7} + fmt.Println(v) + + p := []bool{true, false, false, true, false} + fmt.Println(p) + + + s := []struct { + umur int + fact bool + }{ + {9, true}, + {11, true}, + {4, false}, + {6, true}, + {2, true}, + {3, false}, + {5, true}, + {7, true}, + {11, false}, + {13, true}, + } + fmt.Println(s) +} diff --git a/sidik/sidik.go b/sidik/sidik.go index e7e34eb..e3b599b 100644 --- a/sidik/sidik.go +++ b/sidik/sidik.go @@ -1,5 +1,7 @@ package sidik +/* بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم */ + import "fmt" import "math" @@ -37,6 +39,31 @@ func Main() { flow12() flow13() flow14() + fmt.Printf("\n") + type1() + type2() + type3() + type4() + type5() + type6() + type7() + type8() + type9() + type10() + type11() + type12() + type13() + type14() + type15() + type16() + type17() + type18() + type19() + type20() + type21() + type22() + type23() + type24() } func basic1() {