W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
常用基本類型的轉(zhuǎn)換方法比較簡單,我們這里使用一個(gè)例子來演示轉(zhuǎn)換方法的使用及效果。
更多的類型轉(zhuǎn)換方法請(qǐng)參考接口文檔:https://pkg.go.dev/github.com/gogf/gf/v2/util/gconv
package main
import (
"fmt"
"github.com/gogf/gf/v2/util/gconv"
)
func main() {
i := 123.456
fmt.Printf("%10s %v\n", "Int:", gconv.Int(i))
fmt.Printf("%10s %v\n", "Int8:", gconv.Int8(i))
fmt.Printf("%10s %v\n", "Int16:", gconv.Int16(i))
fmt.Printf("%10s %v\n", "Int32:", gconv.Int32(i))
fmt.Printf("%10s %v\n", "Int64:", gconv.Int64(i))
fmt.Printf("%10s %v\n", "Uint:", gconv.Uint(i))
fmt.Printf("%10s %v\n", "Uint8:", gconv.Uint8(i))
fmt.Printf("%10s %v\n", "Uint16:", gconv.Uint16(i))
fmt.Printf("%10s %v\n", "Uint32:", gconv.Uint32(i))
fmt.Printf("%10s %v\n", "Uint64:", gconv.Uint64(i))
fmt.Printf("%10s %v\n", "Float32:", gconv.Float32(i))
fmt.Printf("%10s %v\n", "Float64:", gconv.Float64(i))
fmt.Printf("%10s %v\n", "Bool:", gconv.Bool(i))
fmt.Printf("%10s %v\n", "String:", gconv.String(i))
fmt.Printf("%10s %v\n", "Bytes:", gconv.Bytes(i))
fmt.Printf("%10s %v\n", "Strings:", gconv.Strings(i))
fmt.Printf("%10s %v\n", "Ints:", gconv.Ints(i))
fmt.Printf("%10s %v\n", "Floats:", gconv.Floats(i))
fmt.Printf("%10s %v\n", "Interfaces:", gconv.Interfaces(i))
}
執(zhí)行后,輸出結(jié)果為:
Int: 123
Int8: 123
Int16: 123
Int32: 123
Int64: 123
Uint: 123
Uint8: 123
Uint16: 123
Uint32: 123
Uint64: 123
Float32: 123.456
Float64: 123.456
Bool: true
String: 123.456
Bytes: [119 190 159 26 47 221 94 64]
Strings: [123.456]
Ints: [123]
Floats: [123.456]
Interfaces: [123.456]
數(shù)字轉(zhuǎn)換方法例如?gconv.Int/Uint
?等等,當(dāng)給定的轉(zhuǎn)換參數(shù)為字符串時(shí),會(huì)自動(dòng)識(shí)別十六進(jìn)制、八進(jìn)制。
?gconv
?將前導(dǎo)?0
?的數(shù)字字符串當(dāng)做八進(jìn)制轉(zhuǎn)換。例如,?gconv.Int("010")
?將會(huì)返回?8
?。
?gconv
?將?0x
?開頭的數(shù)字字符串當(dāng)做十六進(jìn)制轉(zhuǎn)換。例如,?gconv.Int("0xff")
?將會(huì)返回?255
?。
特別注意,?gconv
?對(duì)于前導(dǎo)?0
開頭的字符串處理與標(biāo)準(zhǔn)庫的?strconv
?包不一樣:?gconv
?將前導(dǎo)?0
?的數(shù)字字符串當(dāng)做八進(jìn)制轉(zhuǎn)換,而?strconv
?將會(huì)自動(dòng)去掉前導(dǎo)?0
?并按照十進(jìn)制進(jìn)行轉(zhuǎn)換。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: