The base type
- Int, int8, int16, int32, int64
- Float32, float64
- string
- bool
- byte
int2string
strconv.FormatInt(int64,base)
fmt.Sprintf("%v",int)#format
fmt.Sprint(int)
Copy the code
string2int
Func ParseInt64(s string) (int64, error) {result, err := strconv.ParseInt(s, 10, 64)//base indicates basereturn result, err
}
func ParseInt64Silent(s string, defaultValue int64) int64 {
result, err := ParseInt64(s)
iferr ! = nil {return defaultValue
}
return result
}
Copy the code
// Atoi is equivalent to ParseInt(s, 10, 0), converted to type int.
a := strconv.Atoi("1")// Returns an intCopy the code
int2int
var a int32
var b int64
var c uint64
c := int64(a)
d := int32(b)# Loss of accuracy, use with caution
e := int64(c)# The underlying data has not changed, just the interpretation
Copy the code
string2byte
Data := []byte{116,101,115,116} STR := string(data) STR :="test"
data := []byte(str)
Copy the code