golang 各种数据类型转换
golang 各种数据类型转换 Conversions are expressions of the form T(x) where T is a type and x is an expression that can be converted to type T. string > duration d, e := time.ParseDuration("-1h") d, e := time.ParseDuration("1000ms") array > slice arr := [3]int{1,2,3} sli := arr[:] hex > int, big.Int // int n, err := strconv.ParseUint(val, 16, 32) // big.Int n := new(big.Int) n, _ = n.SetString(hex[2:], 16) float > int //float64 转成转成int64 var x float64 = 5.7 var y int = int64(x) int(math.Floor(f + 0.5)) base64 > hex p, err := base64.StdEncoding.DecodeString("QVJWSU4=") if err != nil { // handle error } h := hex.EncodeToString(p) fmt.Println(h) // prints 415256494e bytes > hex hex.EncodeToString(foo) string, float s := "3.1415926535" v1, err := strconv.ParseFloat(v, 32) v2, err := strconv.ParseFloat(v, 64) // float64 > string valueStr = strconv.FormatFloat(v, 'f', 3, 64) int > float i:=5 f:=float32(i) bytes > int var ba = []byte{ 56, 66, 73, 77 } var value int32 value |= int32(ba[0]) value |= int32(ba[1]) << 8 value |= int32(ba[2]) << 16 value |= int32(ba[3]) << 24 reverse the indexing order to switch between big and little endian. int8 > byte 因为两者间的类型及取值范围这些都不相同,不能直接进行转换。int8取值范围为: -128~127,如果要转化的话需要使用bytevalue=256+int8value ...