"Go flex"의 두 판 사이의 차이

(새 문서: ==개요== ;Go flex <syntaxhighlight lang='go' run> package main import ( "fmt" "github.com/jmnote/flex" ) func main() { m := map[string]interface{}{ "ids": []interface{}{...)
 
 
35번째 줄: 35번째 줄:
}
}
</syntaxhighlight>
</syntaxhighlight>
==참고==
* https://github.com/jmnote/flex


[[분류: Go]]
[[분류: Go]]

2022년 4월 14일 (목) 11:55 기준 최신판

1 개요[ | ]

Go flex
package main

import (
	"fmt"

	"github.com/jmnote/flex"
)

func main() {
	m := map[string]interface{}{
		"ids":    []interface{}{1, 2, 3},
		"fruits": []interface{}{"apple", "banana"},
	}
	f := flex.NewFromObject(m)
	fmt.Println(f.Get(".ids"))            // [1 2 3]
	fmt.Println(f.Get(".ids[1]"))         // 2
	fmt.Println(f.GetInt(".ids[1]"))      // 2
	fmt.Println(f.GetIntSlice(".ids")[1]) // 2

	ids := f.GetIntSlice(".ids")
	ids = append(ids, 9)
	fmt.Println(ids) // [1 2 3 9]

	fruits := f.GetStringSlice(".fruits")
	fruits = append(fruits, "pear")
	fmt.Println(fruits) // [apple banana pear]

	m["ids"] = append(f.GetIntSlice(".id"), 4)
	m["fruits"] = append(f.GetStringSlice(".fruits"), "melon")
	fmt.Println(m) // map[fruits:[apple banana melon] ids:[1 2 3 4]]
}

2 참고[ | ]

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