"Collection-go"의 두 판 사이의 차이

(새 문서: ==개요== ;collection.go <syntaxhighlight lang='go' run line> package collection import ( "encoding/json" "reflect" ) func TypeOf(obj interface{}) string { return reflect.TypeO...)
 
1번째 줄: 1번째 줄:
==개요==
==개요==
{{소문자}}
;collection.go
;collection.go



2021년 11월 24일 (수) 17:45 판

개요

collection.go
package collection

import (
	"encoding/json"
	"reflect"
)

func TypeOf(obj interface{}) string {
	return reflect.TypeOf(obj).String()
}

func Collect(bytes []byte) map[string]interface{} {
	m := map[string]interface{}{}
	err := json.Unmarshal(bytes, &m)
	if err != nil {
		return nil
	}
	return m
}

func JsonBytes(obj interface{}) []byte {
	res, _ := json.Marshal(obj)
	return res
}

func JsonString(obj interface{}) string {
	res, _ := json.Marshal(obj)
	return string(res)
}

func Dict(obj interface{}) map[string]interface{} {
	return obj.(map[string]interface{})
}

func List(obj interface{}) []interface{} {
	return obj.([]interface{})
}

func T(obj interface{}, indices ...interface{}) interface{} {
	for _, index := range indices {
		_, isInt := index.(int)
		if isInt {
			obj = obj.([]interface{})[index.(int)]
		} else {
			obj = obj.(map[string]interface{})[index.(string)]
		}
	}
	return obj
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}