golang最近挺火绰姻,試著用了一下枉侧,有些語法需要熟悉,記錄一下
常用數(shù)據(jù)格式
整型
uint8
狂芋、uint16
棵逊、uint32
、uint64
int8
银酗、int16
辆影、int32
、int64
浮點型
float32
黍特、float64
字符串
string
格式轉(zhuǎn)換
strconv 包中常用的函數(shù)包括 Atoi()蛙讥、Itia()、parse 系列函數(shù)灭衷、format 系列函數(shù)次慢、append 系列函數(shù)等
string 與 int 的Atoi()、Itia()
num := strconv.Atoi(str)
str := strconv.Itoa(num)
string 轉(zhuǎn)其他格式的ParseBool()翔曲、ParseFloat()迫像、ParseInt()、ParseUint()
boo1, err := strconv.ParseBool(str1)
num, err := strconv.ParseInt(str, 10, 0)
num, err := strconv.ParseUint(str, 10, 0)
num, err := strconv.ParseFloat(str, 64)
Format 系列函數(shù)實現(xiàn)了將給定類型數(shù)據(jù)格式化為字符串類型的功能瞳遍,其中包括 FormatBool()闻妓、FormatInt()、FormatUint()掠械、FormatFloat()由缆。
str := strconv.FormatBool(num)
str := strconv.FormatInt(num, 16) // 16進制
str := strconv.FormatUint(num, 16) // 16進制
str := strconv.FormatFloat(num, 'f', 6, 64)
Append 系列函數(shù)用于將指定類型轉(zhuǎn)換成字符串后追加到一個切片中,其中包含 AppendBool()猾蒂、AppendFloat()均唉、AppendInt()、AppendUint()肚菠。
Append 系列函數(shù)和 Format 系列函數(shù)的使用方法類似舔箭,只不過是將轉(zhuǎn)換后的結(jié)果追加到一個切片中。
package main
import (
"fmt"
"strconv"
)
func main() {
// 聲明一個slice
b10 := []byte("int (base 10):")
// 將轉(zhuǎn)換為10進制的string蚊逢,追加到slice中
b10 = strconv.AppendInt(b10, -42, 10)
fmt.Println(string(b10))
b16 := []byte("int (base 16):")
b16 = strconv.AppendInt(b16, -42, 16)
fmt.Println(string(b16))
}