Go 맵 슬라이스

(Go 맵 배열에서 넘어옴)

1 개요[ | ]

Go 맵 슬라이스
package main

import "fmt"

func main() {
	fruits := []map[string]interface{}{
		{"id": 102, "name": "apple", "price": 30},
		{"id": 202, "name": "banana", "price": 10},
		{"id": 104, "name": "melon", "price": nil},
	}
	fmt.Println(fruits)
}
package main

import "fmt"

func main() {
	var fruits = []map[string]interface{}{
		{"id": 102, "name": "apple", "price": 20},
		{"id": 202, "name": "banana", "price": 10},
	}
	fruits = append(fruits, map[string]interface{}{"id": 104, "name": "melon", "price": nil})
	fmt.Println(fruits)
}
package main

import "fmt"

func main() {
	var ms = []interface{}{
		map[string]interface{}{"id": 102, "fruit": "apple", "price": 20},
	}
	ms = append(ms,
		map[string]interface{}{"id": 202, "fruit": "banana", "price": 10},
		map[string]interface{}{"id": 104, "fruit": "melon", "price": nil})
	fmt.Println(ms)
}
package main

import "fmt"

func main() {
	var ms []interface{}
	ms = append(ms,
		map[string]interface{}{"id": 102, "fruit": "apple", "price": 20},
		map[string]interface{}{"id": 202, "fruit": "banana", "price": 10},
		map[string]interface{}{"id": 104, "fruit": "melon", "price": nil},
	)
	fmt.Println(ms)
}

2 access elements[ | ]

package main

import "fmt"

func main() {
	var ms = []interface{}{
		map[string]interface{}{"id": 102, "fruit": "apple", "price": 20},
		map[string]interface{}{"id": 202, "fruit": "banana", "price": 10},
		map[string]interface{}{"id": 104, "fruit": "melon", "price": nil},
	}
	fmt.Println(ms)
	fmt.Println(ms[1])
	fmt.Println(ms[1].(map[string]interface{})["fruit"])
}

3 foreach[ | ]

package main

import "fmt"

func main() {
	var ms = []interface{}{
		map[string]interface{}{"id": 102, "fruit": "apple", "price": 20},
		map[string]interface{}{"id": 202, "fruit": "banana", "price": 10},
		map[string]interface{}{"id": 104, "fruit": "melon", "price": nil},
	}
	for k, v := range ms {
		fmt.Println(k, "=>", v)
	}
}
package main

import "fmt"

func main() {
	var ms = []interface{}{
		map[string]interface{}{"id": 102, "fruit": "apple", "price": 20},
		map[string]interface{}{"id": 202, "fruit": "banana", "price": 10},
		map[string]interface{}{"id": 104, "fruit": "melon", "price": nil},
	}
	for i, v := range ms {
		fmt.Println(i, "=>", v.(map[string]interface{})["fruit"])
	}
}
package main

import "fmt"

func main() {
	var ms = []interface{}{
		map[string]interface{}{"id": 102, "fruit": "apple", "price": 20},
		map[string]interface{}{"id": 202, "fruit": "banana", "price": 10},
		map[string]interface{}{"id": 104, "fruit": "melon", "price": nil},
	}
	for _, v := range ms {
		fmt.Println(v.(map[string]interface{})["fruit"])
	}
}

4 unbalanced[ | ]

package main

import "fmt"

func main() {
	var ms = []interface{}{
		map[string]interface{}{"id": 102, "fruit": "apple", "price": 20},
		map[string]interface{}{"id": 202, "fruit": "banana"},
		map[string]interface{}{"fruit": "melon"},
	}
	fmt.Println(ms)
}

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}