Skip to content

Commit

Permalink
feat: add slice type
Browse files Browse the repository at this point in the history
  • Loading branch information
RealYukiSan committed Mar 16, 2023
1 parent 054bff4 commit 13a6cc0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion main.go
Expand Up @@ -10,5 +10,5 @@ func main() {
// runtime.GOMAXPROCS(2)
// test.TimeoutChannel()
// practice.GQLServer()
test.ImplementArray()
test.ImplementSlice()
}
27 changes: 26 additions & 1 deletion test/data_type/array.go
@@ -1,6 +1,9 @@
package test

import "fmt"
import (
"fmt"
"reflect"
)

func ImplementArray() {
var names [4]string
Expand All @@ -25,9 +28,31 @@ func ImplementArray() {
numbers := [...]int{1, 2, 3, 4}

fmt.Println("data array \t:", numbers)
fmt.Println("type \t:", reflect.TypeOf(numbers).Kind())

// multi-dimension array
matrix := [2][3]int{{3, 2, 1}, {1, 2, 3}}

fmt.Println("matrix :", matrix)
}

func ImplementSlice() {
var fruits = make([]string, 2)
fruits[0] = "khuldi"
fruits[1] = "sirsak"
fmt.Println("type \t:", reflect.TypeOf(fruits).Kind())

fruits = []string{"apple", "grape", "banana"}
aFruits := fruits[:3]
// index terakhir pada slice ini menandakan jumlah kapasitas yang ditentukan
bFruits := fruits[2:3:3]
cFruits := fruits[2:]
dFruits := aFruits[:]

cFruits[0] = "changed"
fmt.Println(fruits)
fmt.Println(aFruits)
fmt.Println(bFruits, len(bFruits), cap(bFruits))
fmt.Println(cFruits)
fmt.Println(dFruits)
}

0 comments on commit 13a6cc0

Please sign in to comment.