示例#1
0
文件: example.go 项目: sdming/kiss
func convert() {
	fmt.Println("convert...")

	var data map[string]interface{} = map[string]interface{}{
		"true":   true,
		"-1":     int(-1),
		"-8":     int8(-8),
		"-16":    int16(-16),
		"-32":    int32(-32),
		"-64":    int64(-64),
		"1":      uint(1),
		"8":      uint8(8),
		"16":     uint16(16),
		"32":     uint32(32),
		"64":     int64(64),
		"3.2":    float32(3.2),
		"-6.4":   float64(-6.4),
		"string": "string"}

	for s, t := range data {
		typ := reflect.ValueOf(t).Type()
		v, err := gotype.Atov(s, typ)
		if err == nil {
			fmt.Printf("convert string %s to type %v %v \n", s, typ, v.Interface() == t)
		} else {
			fmt.Printf("convert string %s to type %v error %v \n", s, typ, err)
		}
	}

}
示例#2
0
func testFromStr(t *testing.T, str string, expect interface{}) {
	v, e := gotype.Atov(str, reflect.TypeOf(expect))
	if e != nil {
		t.Errorf("convert %s to type %v error %s", str, reflect.TypeOf(expect).Name(), e)
	}
	if v.Interface() != expect {
		t.Errorf("convert %s to type %v fail, expect %s, actual %v",
			str, reflect.TypeOf(expect).Name(), expect, v)
	}
}