go類型轉(zhuǎn)換

1.最簡單的轉(zhuǎn)換

type_name(expression)
float跟int可以互轉(zhuǎn),但是會(huì)丟失所有精度。

1.1 string轉(zhuǎn)其他類型
//string到int  
int,err:=strconv.Atoi(string)  
//string到int64  
int64, err := strconv.ParseInt(string, 10, 64)  
//字符串到float32/float64
float32, err = ParseFloat(string, 32)  
float64,err = ParseFloat(string,64)
1.2 int轉(zhuǎn)其他類型
//int到string  
string := strconv.Itoa(int)  
//int64到string  
string := strconv.FormatInt(int64,10)  
//int到float
float64 := float64(int)
1.3 float轉(zhuǎn)其他類型
//float轉(zhuǎn)string
string := strconv.FormatFloat(float32, 'E', -1, 32)
//第二個(gè)參數(shù)可選'f'/'e'/'E'等,含義如下:
// 'b' (-ddddp±ddd,二進(jìn)制指數(shù))
// 'e' (-d.dddde±dd,十進(jìn)制指數(shù))
// 'E' (-d.ddddE±dd,十進(jìn)制指數(shù))
// 'f' (-ddd.dddd,沒有指數(shù))
// 'g' ('e':大指數(shù),'f':其它情況)
// 'G' ('E':大指數(shù),'f':其它情況)

//float轉(zhuǎn)int/int64
float64 := int64(float64)//會(huì)發(fā)生精度丟失

2. strconv包的使用

func AppendBool

func AppendBool(dst []byte, b bool) []byte
AppendBool 根據(jù) b 的值將“true”或“false”附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

func AppendFloat

func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte
AppendFloat 將由 FormatFloat 生成的浮點(diǎn)數(shù) f 的字符串形式附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

func AppendInt

func AppendInt(dst []byte, i int64, base int) []byte
AppendInt 將由 FormatInt 生成的整數(shù)i的字符串形式附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

func AppendQuote

func AppendQuote(dst []byte, s string) []byte
AppendQuote 將由 Quote 生成的代表 s 的雙引號(hào) Go 字符串文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

func AppendQuoteRune

func AppendQuoteRune(dst []byte, r rune) []byte
AppendQuoteRune 將由 QuoteRune 生成的表示符文的單引號(hào) Go 字符文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

func AppendQuoteRuneToASCII

func AppendQuoteRuneToASCII(dst []byte, r rune) []byte
AppendQuoteRuneToASCII 將由 QuoteRuneToASCII 生成的代表該符文的單引號(hào) Go 字符文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

func AppendQuoteRuneToGraphic

func AppendQuoteRuneToGraphic(dst []byte, r rune) []byte
AppendQuoteRuneToGraphic 將由 QuoteRuneToGraphic 生成的表示符文的單引號(hào) Go 字符文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

func AppendQuoteToASCII

func AppendQuoteToASCII(dst []byte, s string) []byte
AppendQuoteToASCII 將由 QuoteToASCII 生成的代表 s 的雙引號(hào) Go 字符串文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

func AppendQuoteToGraphic

func AppendQuoteToGraphic(dst []byte, s string) []byte
AppendQuoteToGraphic 將由 QuoteToGraphic 生成的代表 s 的雙引號(hào) Go 字符串文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

func AppendUint

func AppendUint(dst []byte, i uint64, base int) []byte
AppendUint 將由 FormatUint 生成的無符號(hào)整數(shù) i 的字符串形式附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

func Atoi

func Atoi(s string) (int, error)
Atoi 返回 ParseInt(s, 10, 0) 轉(zhuǎn)換為 int 類型的結(jié)果。

func CanBackquote

func CanBackquote(s string) bool
CanBackquote 報(bào)告字符串 s 是否可以不改變?yōu)閱涡蟹匆?hào)字符串,而不包含 tab 以外的控制字符。

func FormatBool

func FormatBool(b bool) string
FormatBool 根據(jù) b 的值返回“true”或“false”

func FormatFloat

func FormatFloat(f float64, fmt byte, prec, bitSize int) string
FormatFloat 根據(jù)格式 fmt 和 precision prec 將浮點(diǎn)數(shù)f轉(zhuǎn)換為字符串。它將結(jié)果進(jìn)行四舍五入,假設(shè)原始數(shù)據(jù)是從 bitSize 位的浮點(diǎn)值獲得的(float32為32,float64為64)。

格式 fmt 是 'b'(-ddddp±ddd,二進(jìn)制指數(shù)),'e'(-d.dddde±dd,十進(jìn)制指數(shù)),'E'(-d.ddddE±dd,十進(jìn)制指數(shù)),'f'(-ddd.dddd,無指數(shù)),'g'('e'表示大指數(shù),'f'表示否則)或 'G'('E'表示大指數(shù),否則'f')。

precision prec 控制由 'e','E','f','g' 和 'G' 格式打印的位數(shù)(不包括指數(shù))。對于 'e','E' 和 'f',它是小數(shù)點(diǎn)后的位數(shù)。對于 'g' 和 'G' 這是總位數(shù)。特殊精度-1使用必需的最小位數(shù),以便 ParseFloat 完全返回 f 。

func FormatInt

func FormatInt(i int64, base int) string
FormatInt 返回給定基數(shù)中的i的字符串表示,對于2 <= base <= 36.結(jié)果對于數(shù)字值> = 10使用小寫字母 'a' 到 'z' 。

func FormatUint

func FormatUint(i uint64, base int) string
FormatUint 返回給定基數(shù)中的 i 的字符串表示,對于2 <= base <= 36.結(jié)果對于數(shù)字值> = 10使用小寫字母 'a' 到 'z' 。

func IsGraphic

func IsGraphic(r rune) bool
IsGraphic 報(bào)告符文是否被 Unicode 定義為 Graphic。這些字符包括類別 L,M,N,P,S 和 Z 中的字母,標(biāo)記,數(shù)字,標(biāo)點(diǎn),符號(hào)和空格。

func IsPrint

func IsPrint(r rune) bool
IsPrint 報(bào)告該符文是否被 Go 定義為可打印,其定義與 unicode.IsPrint 相同:字母,數(shù)字,標(biāo)點(diǎn),符號(hào)和 ASCII 空格。

func Itoa

func Itoa(i int) string
Itoa 是 FormatInt(int64(i), 10) 的縮寫。

func ParseBool

func ParseBool(str string) (bool, error)
ParseBool 返回字符串表示的布爾值。它接受1,t,T,TRUE,true,True,0,f,F(xiàn),F(xiàn)ALSE,false,F(xiàn)alse。任何其他值都會(huì)返回錯(cuò)誤。

func ParseFloat

func ParseFloat(s string, bitSize int) (float64, error)
ParseFloat 將字符串 s 轉(zhuǎn)換為浮點(diǎn)數(shù),精度由 bitSize:32指定,float32為64; float64為64。當(dāng) bitSize = 32時(shí),結(jié)果仍然具有 float64 類型,但可以在不更改其值的情況下將其轉(zhuǎn)換為 float32。

如果s格式良好且接近有效的浮點(diǎn)數(shù),則 ParseFloat 返回使用 IEEE754 無偏舍入舍入的最近浮點(diǎn)數(shù)。

ParseFloat 返回的錯(cuò)誤具有具體類型 * NumError 并包含 err.Num = s。

如果 s 在語法上不是格式良好的,ParseFloat 返回 err.Err = ErrSyntax。

如果 s 在語法上格式良好,但距離給定大小的最大浮點(diǎn)數(shù)大于1/2 ULP,則 ParseFloat 返回 f =±Inf,err.Err = ErrRange。

func ParseInt

func ParseInt(s string, base int, bitSize int) (i int64, err error)
ParseInt 解釋給定基礎(chǔ)(2到36)中的字符串 s 并返回相應(yīng)的值 i。如果 base == 0,則基數(shù)由字符串的前綴隱含:base 16代表“0x”,base 8代表“0”,否則以10為底數(shù)。

bitSize 參數(shù)指定結(jié)果必須適合的整數(shù)類型。位大小 0,8,16,32 和 64 對應(yīng)于 int,int8,int16,int32 和 int64。

ParseInt 返回的錯(cuò)誤具有具體類型 * NumError 并包含err.Num = s。如果s為空或包含無效數(shù)字,則 err.Err = ErrSyntax,返回值為0; 如果與s對應(yīng)的值不能用給定大小的有符號(hào)整數(shù)表示,則 err.Err = ErrRange,返回的值是相應(yīng) bitSize 和符號(hào)的最大幅度整數(shù)。

func ParseUint

func ParseUint(s string, base int, bitSize int) (uint64, error)
ParseUint 就像 ParseInt,但是對于無符號(hào)數(shù)字。

func Quote

func Quote(s string) string
Quote 返回一個(gè)雙引號(hào)的 Go 字符串字面表示s。返回的字符串使用 Go 轉(zhuǎn)義序列 (\t, \n, \xFF, \u0100) 作為 IsPrint 定義的控制字符和非可打印字符。

func QuoteRune

func QuoteRune(r rune) string
QuoteRune 返回一個(gè)表示符文的單引號(hào) Go 字符。返回的字符串使用 Go 轉(zhuǎn)義序列(\t, \n, \xFF, \u0100) 作為 IsPrint 定義的控制字符和非可打印字符。

func QuoteRuneToASCII

func QuoteRuneToASCII(r rune) string
QuoteRuneToASCII 返回表示符文的單引號(hào) Go 字符。對于非 ASCII 字符和 IsPrint 定義的非可打印字符,返回的字符串使用 Go 轉(zhuǎn)義序列 (\t, \n, \xFF, \u0100)。

func QuoteRuneToGraphic

func QuoteRuneToGraphic(r rune) string
QuoteRuneToGraphic 返回代表符文的單引號(hào) Go 字符。對于非 ASCII 字符和 IsGraphic 定義的非可打印字符,返回的字符串使用Go轉(zhuǎn)義序列 (\t, \n, \xFF, \u0100)。

func QuoteToASCII

func QuoteToASCII(s string) string
QuoteToASCII 返回一個(gè)代表 s 的雙引號(hào) Go 字符串。對于非 ASCII 字符和 IsPrint 定義的非可打印字符,返回的字符串使用 Go 轉(zhuǎn)義序列 (\t, \n, \xFF, \u0100) 。

func QuoteToGraphic

func QuoteToGraphic(s string) string
QuoteToGraphic 返回一個(gè)代表 s 的雙引號(hào) Go 字符串。對于非 ASCII 字符和 IsGraphic 定義的非可打印字符,返回的字符串使用 Go 轉(zhuǎn)義序列 (\t, \n, \xFF, \u0100)。

func Unquote

func Unquote(s string) (string, error)
Unquote 將 s 解釋為單引號(hào),雙引號(hào)或反引號(hào)的 Go 字符串文字,返回引用的字符串值。(如果 s 是單引號(hào),它將是一個(gè) Go 字符字面量; Unquote 會(huì)返回相應(yīng)的一個(gè)字符字符串。)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容