W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
常用基本類型的轉換方法比較簡單,我們這里使用一個例子來演示轉換方法的使用及效果。
更多的類型轉換方法請參考接口文檔: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í)行后,輸出結果為:
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ù)字轉換方法例如?gconv.Int/Uint
?等等,當給定的轉換參數(shù)為字符串時,會自動識別十六進制、八進制。
?gconv
?將前導?0
?的數(shù)字字符串當做八進制轉換。例如,?gconv.Int("010")
?將會返回?8
?。
?gconv
?將?0x
?開頭的數(shù)字字符串當做十六進制轉換。例如,?gconv.Int("0xff")
?將會返回?255
?。
特別注意,?gconv
?對于前導?0
開頭的字符串處理與標準庫的?strconv
?包不一樣:?gconv
?將前導?0
?的數(shù)字字符串當做八進制轉換,而?strconv
?將會自動去掉前導?0
?并按照十進制進行轉換。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: