Go 슬라이스 맵

1 개요[ | ]

Go 슬라이스 맵
package main
import "fmt"

func main() {
	sm := map[string]interface{}{
		"ids":       []int{102, 202, 104},
		"names":     []string{"Yonezawa Akinori", "Murata Makoto", "Sakamura Ken"},
		"addresses": []string{"Naha, Okinawa", "Sendai, Miyagi", "Kumamoto, Kumamoto"},
	}
	fmt.Println(sm)
}
package main
import "fmt"

func main() {
	sm := make(map[string]interface{})
	sm["ids"] = []int{102, 202, 104}
	sm["names"] = []string{"Yonezawa Akinori", "Murata Makoto", "Sakamura Ken"}
	sm["addresses"] = []string{"Naha, Okinawa", "Sendai, Miyagi", "Kumamoto, Kumamoto"}
	fmt.Println(sm)
}

2 foreach[ | ]

package main

import "fmt"

func main() {
	sm := map[string]interface{}{
		"ids":    []int{102, 202, 104},
		"fruits": []string{"apple", "banana", "melon"},
	}
	for k, v := range sm {
		fmt.Println(k, v)
	}
}
package main

import "fmt"

func main() {
	sm := map[string]interface{}{
		"ids":    []int{102, 202, 104},
		"fruits": []string{"apple", "banana", "melon"},
	}
	for k, v := range sm["fruits"].([]string) {
		fmt.Println(k, v)
	}
}

3 append[ | ]

package main

import "fmt"

func main() {
	sm := map[string]interface{}{
		"ids":    []int{1, 2, 3},
		"fruits": []string{"apple", "banana"},
	}
	sm["ids"] = append(sm["ids"].([]int), 4)
	sm["fruits"] = append(sm["fruits"].([]string), "melon")
	fmt.Println(sm)
}
package main

import "fmt"

func main() {
	sm := map[string]interface{}{
		"ids":    []int{1, 2, 3},
		"fruits": []string{"apple", "banana"},
	}
	sm["ids"] = append(sm["ids"].([]int), 4, 5, 6, 7, 8)
	sm["fruits"] = append(sm["fruits"].([]string), "melon", "watermelon")
	sm["extras"] = []string{"hello", "world"}
	fmt.Println(sm)
}
package main

import "fmt"

func main() {
	sm := map[string]interface{}{
		"ids":    []interface{}{1, 2, 3},
		"fruits": []interface{}{"apple", "banana"},
	}
	sm["ids"] = append(sm["ids"].([]interface{}), 4)
	sm["fruits"] = append(sm["fruits"].([]interface{}), "melon", "watermelon")
	fmt.Println(sm)
}
package main

import "fmt"

func main() {
	sm := map[string]interface{}{
		"ids":    []interface{}{1, 2, 3},
		"fruits": []interface{}{"apple", "banana"},
	}
	sm["ids"] = append(sm["ids"].([]interface{}), "x")
	sm["fruits"] = append(sm["fruits"].([]interface{}), -1)
	fmt.Println(sm)
}

4 같이 보기[ | ]

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