一四零、go类型转换

Interface{} 转int

//go中interface类型很好用,但是有时候用了就没办法作为键值了,介绍一种转换方法
func GetInterfaceToInt(t1 interface{}) int {
	var t2 int
	switch t1.(type) {
	case uint:
		t2 = int(t1.(uint))
		break
	case int8:
		t2 = int(t1.(int8))
		break
	case uint8:
		t2 = int(t1.(uint8))
		break
	case int16:
		t2 = int(t1.(int16))
		break
	case uint16:
		t2 = int(t1.(uint16))
		break
	case int32:
		t2 = int(t1.(int32))
		break
	case uint32:
		t2 = int(t1.(uint32))
		break
	case int64:
		t2 = int(t1.(int64))
		break
	case uint64:
		t2 = int(t1.(uint64))
		break
	case float32:
		t2 = int(t1.(float32))
		break
	case float64:
		t2 = int(t1.(float64))
		break
	case string:
		t2, _ = strconv.Atoi(t1.(string))
		if t2 == 0 && len(t1.(string)) > 0 {
			f, _ := strconv.ParseFloat(t1.(string), 64)
			t2 = int(f)
		}
		break
	case nil:
		t2 = 0
		break
	case json.Number:
		t3, _ := t1.(json.Number).Int64()
		t2 = int(t3)
		break
	default:
		t2 = t1.(int)
		break
	}
	return t2
}

string、int互转

// 1. 字符串转int:Atoi()
	i, _ := strconv.Atoi("1")
	println(1 + i) // 2

	// Atoi()转换失败
	i, err := strconv.Atoi("a")
	if err != nil {
		fmt.Printf("err: %v\n", err)
	}
// 2. int转字符串:Itoa()
	s := strconv.Itoa(99)
	println(s + "1") //991

你可能感兴趣的:(后端,golang,golang,开发语言,后端)