Go flex

Jmnote (토론 | 기여)님의 2022년 4월 14일 (목) 11:54 판 (새 문서: ==개요== ;Go flex <syntaxhighlight lang='go' run> package main import ( "fmt" "github.com/jmnote/flex" ) func main() { m := map[string]interface{}{ "ids": []interface{}{...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

개요

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]]
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}