Sun, 8 Mar 2015 11:41 pm
Kesimpulan array #go sampai dengan hari ini

package main

    import "fmt"
    import "encoding/json"

    type Blog struct {
        id string
        things string
        tag []string
    }

    func main() {
        
        fmt.Println("\nMari mulai belajar list dan map Golang ...")

        // list
        listA   := []string{"ikan","sayur","buah", "1", "2", "3"}
        listB, _ := json.Marshal(listA)
        fmt.Println("\nlistA adalah " + string(listB))

        listA2   := []int{1,2,3}
        listB2, _ := json.Marshal(listA2)
        fmt.Println("\nlistA2 adalah " + string(listB2))   

        // dict
        mapA    := map[string]int {"apple": 5, "lettuce": 7}
        mapB, _ := json.Marshal(mapA)
        fmt.Println("\nmapA adalah " + string(mapB))

        fmt.Println("\nlist A")
        for _, value := range listA {
            fmt.Println(value)
        }

        fmt.Println("\nlist A2")
        for _, value := range listA2 {
            fmt.Println(value)
        }      

        fmt.Println("\nmapA")
        for key , value := range mapA {
            fmt.Println(key + " " + fmt.Sprint(value))
        }

        // manipulate list
        fmt.Println("\nAppend listA")
        x := append(listA, "77")
        fmt.Println(x)

        // modify semua listA
        fmt.Println("\nModify listA")
        var modListA []string
        for _, value := range listA {
            modListA = append(modListA, "mod:" + value)
        }
        fmt.Println(modListA)
    }

script diatas menghasilkan output

D:\PROJECTS\GO-APP>go run coba_json_sendiri.go

Mari mulai belajar list dan map Golang ...

listA adalah ["ikan","sayur","buah","1","2","3"]

listA2 adalah [1,2,3]

mapA adalah {"apple":5,"lettuce":7}

list A
ikan
sayur
buah
1
2
3

list A2
1
2
3

mapA
lettuce 7
apple 5

Append listA
[ikan sayur buah 1 2 3 77]

Modify listA
[mod:ikan mod:sayur mod:buah mod:1 mod:2 mod:3]

40
Additional Info: