類型轉(zhuǎn)換

零值

變量在定義時沒有明確的初始化時會賦值為 零值 涝动。
零值是:

數(shù)值類型為 0 扣孟,
布爾類型為 false 良瞧,
字符串為 "" (空字符串)。

Golang 不支持隱式類型轉(zhuǎn)換悄晃,即便是從窄向?qū)掁D(zhuǎn)換也不行玫霎。

package main

var b byte = 100

// var n int = b
// ./main.go:5:5: cannot use b (type byte) as type int in assignment
var n int = int(b) // 顯式轉(zhuǎn)換

func main() {}

同樣不能將其他類型當(dāng) bool 值使用。

package main

func main() {
    a := 100
    if a { // Error: non-bool a (type int) used as if condition
        println("true")
    }
}

類型轉(zhuǎn)換

類型轉(zhuǎn)換用于將一種數(shù)據(jù)類型的變量轉(zhuǎn)換為另外一種類型的變量妈橄。Go 語言類型轉(zhuǎn)換基本格式如下:
表達式 T(v) 將值 v 轉(zhuǎn)換為類型 T 庶近。
type_name(expression)
type_name 為類型,expression 為表達式眷蚓。
實例
將整型轉(zhuǎn)化為浮點型鼻种,并計算結(jié)果,將結(jié)果賦值給浮點型變量:

package main

import "fmt"

func main() {
    var sum int = 17
    var count int = 5
    var mean float32

    mean = float32(sum) / float32(count)
    fmt.Printf("mean 的值為: %f\n", mean)
}

輸出結(jié)果:

mean 的值為: 3.400000

一些關(guān)于數(shù)值的轉(zhuǎn)換:

package main

import (
    "fmt"
    "reflect"
)
func main() {
    var i int = 42
    fmt.Printf("i value is : %v , type is : %v \n", i, reflect.TypeOf(i))
    var f float64 = float64(i)
    fmt.Printf("f value is : %v , type is : %v \n", f, reflect.TypeOf(f))
    var u uint = uint(f)
    fmt.Printf("u value is : %v , type is : %v \n", u, reflect.TypeOf(u))
}

輸出結(jié)果:

i value is : 42 , type is : int 
f value is : 42 , type is : float64 
u value is : 42 , type is : uint 

或者沙热,更加簡單的形式:

package main

import (
    "fmt"
    "reflect"
)

func main() {
    i := 42
    f := float64(i)
    u := uint(f)
    fmt.Printf("i value is : %v , type is : %v \n", i, reflect.TypeOf(i))
    fmt.Printf("f value is : %v , type is : %v \n", f, reflect.TypeOf(f))
    fmt.Printf("u value is : %v , type is : %v \n", u, reflect.TypeOf(u))
}

輸出結(jié)果:

i value is : 42 , type is : int 
f value is : 42 , type is : float64 
u value is : 42 , type is : uint 

類型推導(dǎo)

在定義一個變量卻并不顯式指定其類型時(使用 := 語法或者 var = 表達式語法)【全局變量不適用】叉钥, 變量的類型由(等號)右側(cè)的值推導(dǎo)得出。
當(dāng)右值定義了類型時篙贸,新變量的類型與其相同:

package main

import (
    "fmt"
    "reflect"
)

func main() {
    var i int
    j := i // j 也是一個 int
    fmt.Printf("i type is : %v\n", reflect.TypeOf(i))
    fmt.Printf("j type is : %v\n", reflect.TypeOf(j))
}

輸出結(jié)果:

i type is : int
j type is : int

但是當(dāng)右邊包含了未指名類型的數(shù)字常量時投队,新的變量就可能是 int 、 float64 或 complex128 爵川。

package main

import (
    "fmt"
    "reflect"
)

func main() {
    i := 42           
    f := 3.142        
    g := 0.867 + 0.5i 
    fmt.Printf("i type is : %v\n", reflect.TypeOf(i))
    fmt.Printf("f type is : %v\n", reflect.TypeOf(f))
    fmt.Printf("g type is : %v\n", reflect.TypeOf(g))
}

輸出結(jié)果:

i type is : int
f type is : float64
g type is : complex128

Go各種類型轉(zhuǎn)換及函數(shù)的高級用法

字符串轉(zhuǎn)整形

將字符串轉(zhuǎn)換為 int 類型 
strconv.ParseInt(str,base,bitSize)
str:要轉(zhuǎn)換的字符串 
base:進位制(2 進制到 36 進制) 
bitSize:指定整數(shù)類型(0:int敷鸦、8:int8、16:int16寝贡、32:int32扒披、64:int64) 
返回轉(zhuǎn)換后的結(jié)果和轉(zhuǎn)換時遇到的錯誤 
如果 base 為 0,則根據(jù)字符串的前綴判斷進位制(0x:16圃泡,0:8谎碍,其它:10)
ParseUint 功能同 ParseInt 一樣,只不過返回 uint 類型整數(shù)

Atoi 相當(dāng)于 ParseInt(s, 10, 0)
通常使用這個函數(shù)洞焙,而不使用 ParseInt
該方法的源碼是:

// Itoa is shorthand for FormatInt(i, 10).
func Itoa(i int) string {
    return FormatInt(int64(i), 10)
}

可以看出是FormatInt方法的簡單實現(xiàn)。
package main

import (
    "fmt"
    "reflect"
    "strconv"
)

func main() {
    i, ok := strconv.ParseInt("1000", 10, 0)
    if ok == nil {
        fmt.Printf("ParseInt , i is %v , type is %v\n", i, reflect.TypeOf(i))
    }

    ui, ok := strconv.ParseUint("100", 10, 0)
    if ok == nil {
        fmt.Printf("ParseUint , ui is %v , type is %v\n", ui, reflect.TypeOf(i))
    }

    oi, ok := strconv.Atoi("100")
    if ok == nil {
        fmt.Printf("Atoi , oi is %v , type is %v\n", oi, reflect.TypeOf(i))
    }

}

輸出結(jié)果:

ParseInt , i is 1000 , type is int64
ParseUint , ui is 100 , type is int64
Atoi , oi is 100 , type is int64

整形轉(zhuǎn)字符串

FormatInt int 型整數(shù) i 轉(zhuǎn)換為字符串形式 
strconv.FormatInt.(i,base)
FormatUint 將 uint 型整數(shù) i 轉(zhuǎn)換為字符串形式 
strconv.FormatUint.(i,base)
base:進位制(2 進制到 36 進制) 
大于 10 進制的數(shù),返回值使用小寫字母 ‘a(chǎn)’ 到 ‘z’
Itoa 相當(dāng)于 FormatInt(i, 10)
package main

import (
    "fmt"
    "reflect"
    "strconv"
)

func main() {
    var i int64
    i = 0x100
    str := strconv.FormatInt(i, 10) // FormatInt第二個參數(shù)表示進制澡匪,10表示十進制熔任。
    fmt.Println(str)
    fmt.Println(reflect.TypeOf(str))
}

輸出結(jié)果:

256
string

AppendInt 將 int 型整數(shù) i 轉(zhuǎn)換為字符串形式,并追加到 []byte 的尾部
strconv.AppendInt([]byte, i, base)
AppendUint 將 uint 型整數(shù) i 轉(zhuǎn)換為字符串形式唁情,并追加到 dst 的尾部
strconv.AppendUint([]byte, i, base)
i:要轉(zhuǎn)換的字符串
base:進位制
返回追加后的 []byte

package main

import (
    "fmt"
    "strconv"
)

func main() {
    b := make([]byte, 0)
    b = strconv.AppendInt(b, -2048, 16)
    fmt.Printf("%s\n", b)
}

輸出結(jié)果:

-800

字節(jié)轉(zhuǎn)32位整形

package main

import (
    "bytes"
    "encoding/binary"
    "fmt"
)

func main() {
    b := []byte{0x00, 0x00, 0x03, 0xe8}
    bytesBuffer := bytes.NewBuffer(b)

    var x int32
    binary.Read(bytesBuffer, binary.BigEndian, &x)
    fmt.Println(x)
}
// 其中binary.BigEndian表示字節(jié)序疑苔,相應(yīng)的還有l(wèi)ittle endian。通俗的說法叫大端甸鸟、小端惦费。

輸出結(jié)果:

1000

32位整形轉(zhuǎn)字節(jié)

package main

import (
    "bytes"
    "encoding/binary"
    "fmt"
    "reflect"
)

func main() {
    var x int32
    x = 106
    bytesBuffer := bytes.NewBuffer([]byte{})
    binary.Write(bytesBuffer, binary.BigEndian, x)
    b := bytesBuffer.Bytes()
    fmt.Println(b)
    fmt.Println(reflect.TypeOf(b))
}

輸出結(jié)果:

[0 0 0 106]
[]uint8

字節(jié)轉(zhuǎn)字符串

package main

import (
    "fmt"
    "reflect"
)

func main() {
    b := []byte{97, 98, 99, 100}
    str := string(b)
    fmt.Println(str)
    fmt.Println(reflect.TypeOf(str))
}

輸出結(jié)果:

abcd
string

字符串轉(zhuǎn)字節(jié)

package main

import (
    "fmt"
)

func main() {
    str := "abcd"
    b := []byte(str)
    fmt.Println(b)
}

輸出結(jié)果:

[97 98 99 100]

字符串轉(zhuǎn)布爾值 ParseBool

package main

import (
    "fmt"
    "strconv"
)

func main() {
    b, err := strconv.ParseBool("1")
    fmt.Printf("string 1 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
    b, err = strconv.ParseBool("t")
    fmt.Printf("string t 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
    b, err = strconv.ParseBool("T")
    fmt.Printf("string T 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
    b, err = strconv.ParseBool("true")
    fmt.Printf("string true 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
    b, err = strconv.ParseBool("True")
    fmt.Printf("string True 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
    b, err = strconv.ParseBool("TRUE")
    fmt.Printf("string TRUE 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
    b, err = strconv.ParseBool("TRue")
    fmt.Printf("string TRue 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
    b, err = strconv.ParseBool("")
    fmt.Printf("string '' 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
    b, err = strconv.ParseBool("0")
    fmt.Printf("string 0 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
    b, err = strconv.ParseBool("f")
    fmt.Printf("string f 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
    b, err = strconv.ParseBool("F")
    fmt.Printf("string F 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
    b, err = strconv.ParseBool("false")
    fmt.Printf("string false 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
    b, err = strconv.ParseBool("False")
    fmt.Printf("string False 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
    b, err = strconv.ParseBool("FALSE")
    fmt.Printf("string FALSE 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
    b, err = strconv.ParseBool("FALse")
    fmt.Printf("string FALse 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
    b, err = strconv.ParseBool("abc")
    fmt.Printf("string abc 轉(zhuǎn) bool :%v , err is : %v\n", b, err)
}

輸出結(jié)果:

string 1 轉(zhuǎn) bool :true , err is : <nil>
string t 轉(zhuǎn) bool :true , err is : <nil>
string T 轉(zhuǎn) bool :true , err is : <nil>
string true 轉(zhuǎn) bool :true , err is : <nil>
string True 轉(zhuǎn) bool :true , err is : <nil>
string TRUE 轉(zhuǎn) bool :true , err is : <nil>
string TRue 轉(zhuǎn) bool :false , err is : strconv.ParseBool: parsing "TRue": invalid syntax
string '' 轉(zhuǎn) bool :false , err is : strconv.ParseBool: parsing "": invalid syntax
string 0 轉(zhuǎn) bool :false , err is : <nil>
string f 轉(zhuǎn) bool :false , err is : <nil>
string F 轉(zhuǎn) bool :false , err is : <nil>
string false 轉(zhuǎn) bool :false , err is : <nil>
string False 轉(zhuǎn) bool :false , err is : <nil>
string FALSE 轉(zhuǎn) bool :false , err is : <nil>
string FALse 轉(zhuǎn) bool :false , err is : strconv.ParseBool: parsing "FALse": invalid syntax
string abc 轉(zhuǎn) bool :false , err is : strconv.ParseBool: parsing "abc": invalid syntax

ParseBool 將字符串轉(zhuǎn)換為布爾值 
它接受真值:1, t, T, TRUE, true, True 
它接受假值:0, f, F, FALSE, false, False. 
其它任何值都返回一個錯誤

布爾值轉(zhuǎn)換為字符串 FormatBool

package main

import (
    "fmt"
    "reflect"
    "strconv"
)

func main() {
    t := strconv.FormatBool(true)
    f := strconv.FormatBool(false)
    fmt.Printf("t is %v , t type is %v\n", t, reflect.TypeOf(t))
    fmt.Printf("f is %v , f type is %v\n", f, reflect.TypeOf(f))
}

輸出結(jié)果:

t is true , t type is string
f is false , f type is string

AppendBool 將布爾類型轉(zhuǎn)換為字符串
然后將結(jié)果追加到 []byte 的尾部,返回追加后的 []byte

package main

import (
    "fmt"
    "strconv"
)

func main() {
    rst := []byte{}
    fmt.Printf("[]byte{} is %s\n", rst)
    rst = strconv.AppendBool(rst, true)
    fmt.Printf("appended true []byte{} is %s\n", rst)
    rst = strconv.AppendBool(rst, false)
    fmt.Printf("appended false []byte{} is %s\n", rst)
}

輸出結(jié)果:

[]byte{} is 
appended true []byte{} is true
appended false []byte{} is truefalse

將字符串轉(zhuǎn)換為浮點數(shù)
strconv.ParseFloat(str,bitSize)
str:要轉(zhuǎn)換的字符串
bitSize:指定浮點類型(32:float32抢韭、64:float64)
如果 str 是合法的格式薪贫,而且接近一個浮點值,
則返回浮點數(shù)的四舍五入值(依據(jù) IEEE754 的四舍五入標(biāo)準(zhǔn))
如果 str 不是合法的格式刻恭,則返回“語法錯誤”
如果轉(zhuǎn)換結(jié)果超出 bitSize 范圍瞧省,則返回“超出范圍”

package main

import (
    "fmt"
    "strconv"
)

func main() {
    s := "0.12345678901234567890"
    f, err := strconv.ParseFloat(s, 32)
    fmt.Println(f, err)
    fmt.Println(float32(f))
    fmt.Println("-----")
    f, err = strconv.ParseFloat(s, 64)
    fmt.Println(f, err)
    fmt.Println(float64(f))
    fmt.Println("-----")
    str := "abcd"
    f, err = strconv.ParseFloat(str, 32)
    fmt.Println(f, err)
}

輸出結(jié)果:

0.12345679104328156 <nil>
0.12345679
-----
0.12345678901234568 <nil>
0.12345678901234568
-----
0 strconv.ParseFloat: parsing "abcd": invalid syntax

將浮點數(shù)轉(zhuǎn)換為字符串值:
strconv.FormatFloat(f,fmt,prec,bitSize)
f:要轉(zhuǎn)換的浮點數(shù)
fmt:格式標(biāo)記(b、e鳍贾、E鞍匾、,f、g骑科、G)
prec:精度(數(shù)字部分的長度橡淑,不包括指數(shù)部分)
bitSize:指定浮點類型(32:float32、64:float64)

格式標(biāo)記:
‘b’ (-ddddp±ddd咆爽,二進制指數(shù))
‘e’ (-d.dddde±dd梁棠,十進制指數(shù))
‘E’ (-d.ddddE±dd,十進制指數(shù))
‘f’ (-ddd.dddd伍掀,沒有指數(shù))
‘g’ (‘e’:大指數(shù)掰茶,’f’:其它情況)
‘G’ (‘E’:大指數(shù),’f’:其它情況)

如果格式標(biāo)記為 ‘e’蜜笤,’E’和’f’濒蒋,則 prec 表示小數(shù)點后的數(shù)字位數(shù)
如果格式標(biāo)記為 ‘g’,’G’把兔,則 prec 表示總的數(shù)字位數(shù)(整數(shù)部分+小數(shù)部分)

package main

import (
    "fmt"
    "strconv"
)

func main() {
    f := 100.12345678901234567890123456789
    fmt.Println(strconv.FormatFloat(f, 'b', 5, 32))
    fmt.Println(strconv.FormatFloat(f, 'e', 5, 32))
    fmt.Println(strconv.FormatFloat(f, 'E', 5, 32))
    fmt.Println(strconv.FormatFloat(f, 'f', 5, 32))
    fmt.Println(strconv.FormatFloat(f, 'g', 5, 32))
    fmt.Println(strconv.FormatFloat(f, 'G', 5, 32))
    fmt.Println(strconv.FormatFloat(f, 'b', 30, 32))
    fmt.Println(strconv.FormatFloat(f, 'e', 30, 32))
    fmt.Println(strconv.FormatFloat(f, 'E', 30, 32))
    fmt.Println(strconv.FormatFloat(f, 'f', 30, 32))
    fmt.Println(strconv.FormatFloat(f, 'g', 30, 32))
    fmt.Println(strconv.FormatFloat(f, 'G', 30, 32))
}

輸出結(jié)果:

13123382p-17
1.00123e+02
1.00123E+02
100.12346
100.12
100.12
13123382p-17
1.001234588623046875000000000000e+02
1.001234588623046875000000000000E+02
100.123458862304687500000000000000
100.1234588623046875
100.1234588623046875

AppendFloat 將浮點數(shù) f 轉(zhuǎn)換為字符串值沪伙,并將轉(zhuǎn)換結(jié)果追加到 []byte 的尾部
返回追加后的 []byte

package main

import (
    "fmt"
    "strconv"
)

func main() {
    f := 100.12345678901234567890123456789
    b := make([]byte, 0)
    b = strconv.AppendFloat(b, f, 'f', 5, 32)
    b = append(b, " "...)
    b = strconv.AppendFloat(b, f, 'e', 5, 32)
    fmt.Printf("%s\n", b)
}

輸出結(jié)果:

100.12346 1.00123e+02

Quote 將字符串 s 轉(zhuǎn)換為“雙引號”引起來的字符串
其中的特殊字符將被轉(zhuǎn)換為“轉(zhuǎn)義字符”
不可顯示的字符”將被轉(zhuǎn)換為“轉(zhuǎn)義字符”

package main

import (
    "fmt"
    "strconv"
)

func main() {
    fmt.Println(strconv.Quote(`C:\Windows`))
}

輸出結(jié)果:

"C:\\Windows"

AppendQuote 將字符串 s 轉(zhuǎn)換為“雙引號”引起來的字符串,
并將結(jié)果追加到 []byte 的尾部县好,返回追加后的 []byte
其中的特殊字符將被轉(zhuǎn)換為“轉(zhuǎn)義字符”

package main

import (
    "fmt"
    "strconv"
)

func main() {
    s := `C:\Windows`
    b := make([]byte, 0)
    b = strconv.AppendQuote(b, s)
    fmt.Printf("%s\n", b)
}

輸出結(jié)果:

"C:\\Windows"

QuoteToASCII 將字符串 s 轉(zhuǎn)換為“雙引號”引起來的 ASCII 字符串
“非 ASCII 字符”和“特殊字符”將被轉(zhuǎn)換為“轉(zhuǎn)義字符”

package main

import (
    "fmt"
    "strconv"
)

func main() {
    asc := strconv.QuoteToASCII("Hello 世界围橡!")
    fmt.Println(asc)
}

輸出結(jié)果:

"Hello \u4e16\u754c\uff01"

AppendQuoteToASCII 將字符串 s 轉(zhuǎn)換為“雙引號”引起來的 ASCII 字符串,
并將結(jié)果追加到 []byte 的尾部缕贡,返回追加后的 []byte
非 ASCII 字符”和“特殊字符”將被轉(zhuǎn)換為“轉(zhuǎn)義字符”

package main

import (
    "fmt"
    "strconv"
)

func main() {
    s := "Hello 世界翁授!"
    b := make([]byte, 0)
    b = strconv.AppendQuoteToASCII(b, s)
    fmt.Printf("%s\n", b)
}

輸出結(jié)果:

"Hello \u4e16\u754c\uff01"

QuoteRune 將 Unicode 字符轉(zhuǎn)換為“單引號”引起來的字符串
特殊字符”將被轉(zhuǎn)換為“轉(zhuǎn)義字符”

package main

import (
    "fmt"
    "strconv"
)

func main() {
    str := strconv.QuoteRune('哈')
    fmt.Println(str)
}

輸出結(jié)果:

'哈'

AppendQuoteRune 將 Unicode 字符轉(zhuǎn)換為“單引號”引起來的字符串拣播,
并將結(jié)果追加到 []byte 的尾部,返回追加后的 []byte
特殊字符”將被轉(zhuǎn)換為“轉(zhuǎn)義字符”

package main

import (
    "fmt"
    "strconv"
)

func main() {
    b := make([]byte, 0)
    b = strconv.AppendQuoteRune(b, '哈')
    fmt.Printf("%s\n", b)
}

輸出結(jié)果:

'哈'

QuoteRuneToASCII 將 Unicode 字符轉(zhuǎn)換為“單引號”引起來的 ASCII 字符串
“非 ASCII 字符”和“特殊字符”將被轉(zhuǎn)換為“轉(zhuǎn)義字符”

package main

import (
    "fmt"
    "strconv"
)

func main() {
    asc := strconv.QuoteRuneToASCII('哈')
    fmt.Println(asc)
}

輸出結(jié)果:

'\u54c8'

AppendQuoteRune 將 Unicode 字符轉(zhuǎn)換為“單引號”引起來的 ASCII 字符串收擦,
并將結(jié)果追加到 []byte 的尾部贮配,返回追加后的 []byte
“非 ASCII 字符”和“特殊字符”將被轉(zhuǎn)換為“轉(zhuǎn)義字符”

package main

import (
    "fmt"
    "strconv"
)

func main() {
    b := make([]byte, 0)
    b = strconv.AppendQuoteRuneToASCII(b, '哈')
    fmt.Printf("%s\n", b)
}

輸出結(jié)果:

'\u54c8'

CanBackquote 判斷字符串 s 是否可以表示為一個單行的“反引號”字符串
字符串中不能含有控制字符(除了 \t)和“反引號”字符,否則返回 false

package main

import (
    "fmt"
    "strconv"
)

func main() {
    b := strconv.CanBackquote("C:\\Windows\n")
    fmt.Printf("\\n is %v\n", b)
    b = strconv.CanBackquote("C:\\Windows\r")
    fmt.Printf("\\r is %v\n", b)
    b = strconv.CanBackquote("C:\\Windows\f")
    fmt.Printf("\\f is %v\n", b)
    b = strconv.CanBackquote("C:\\Windows\t")
    fmt.Printf("\\t is %v\n", b)
    b = strconv.CanBackquote("C:\\Windows`")
    fmt.Printf("` is %v\n", b)
}

輸出結(jié)果:

\n is false
\r is false
\f is false
\t is true
` is false

UnquoteChar 將 s 中的第一個字符“取消轉(zhuǎn)義”并解碼

s:轉(zhuǎn)義后的字符串
quote:字符串使用的“引號符”(用于對引號符“取消轉(zhuǎn)義”)
value: 解碼后的字符
multibyte:value 是否為多字節(jié)字符
tail: 字符串 s 除去 value 后的剩余部分
error: 返回 s 中是否存在語法錯誤

參數(shù) quote 為“引號符”
如果設(shè)置為單引號塞赂,則 s 中允許出現(xiàn) ' 字符泪勒,不允許出現(xiàn)單獨的 ' 字符
如果設(shè)置為雙引號,則 s 中允許出現(xiàn) " 字符宴猾,不允許出現(xiàn)單獨的 " 字符
如果設(shè)置為 0圆存,則不允許出現(xiàn) ' 或 " 字符,可以出現(xiàn)單獨的 ' 或 " 字符

package main

import (
    "fmt"
    "strconv"
)

func main() {
    s := `\"大\\家\\好仇哆!\"`
    c, mb, sr, _ := strconv.UnquoteChar(s, '"')
    fmt.Printf("%-3c %v\n", c, mb)
    for ; len(sr) > 0; c, mb, sr, _ = strconv.UnquoteChar(sr, '"') {
        fmt.Printf("%-3c %v\n", c, mb)
    }

}

輸出結(jié)果:

"   false
"   false
大   true
\   false
家   true
\   false
好   true
沦辙!   true

Unquote 將“帶引號的字符串” s 轉(zhuǎn)換為常規(guī)的字符串(不帶引號和轉(zhuǎn)義字符)
s 可以是“單引號”、“雙引號”或“反引號”引起來的字符串(包括引號本身)
如果 s 是單引號引起來的字符串税产,則返回該該字符串代表的字符

package main

import (
    "fmt"
    "strconv"
)

func main() {
    sr, err := strconv.Unquote("\"大\t家\t好怕轿!\"")
    fmt.Println(sr, err)
    sr, err = strconv.Unquote(`'大家好!'`)
    fmt.Println(sr, err)
    sr, err = strconv.Unquote("'好'")
    fmt.Println(sr, err)
    sr, err = strconv.Unquote("大\\t家\\t好辟拷!")
    fmt.Println(sr, err)
}

輸出結(jié)果:

大   家   好撞羽! <nil>
 invalid syntax
好 <nil>
 invalid syntax

IsPrint 判斷 Unicode 字符 r 是否是一個可顯示的字符
可否顯示并不是你想象的那樣,比如空格可以顯示衫冻,而\t則不能顯示

package main

import (
    "fmt"
    "strconv"
)

func main() {
    fmt.Println(strconv.IsPrint('a'))
    fmt.Println(strconv.IsPrint('好'))
    fmt.Println(strconv.IsPrint(' '))
    fmt.Println(strconv.IsPrint('\t'))
    fmt.Println(strconv.IsPrint('\n'))
    fmt.Println(strconv.IsPrint(0))
}

輸出結(jié)果:

true
true
true
false
false
false
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末诀紊,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子隅俘,更是在濱河造成了極大的恐慌邻奠,老刑警劉巖贺氓,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件罗洗,死亡現(xiàn)場離奇詭異,居然都是意外死亡籍滴,警方通過查閱死者的電腦和手機蒙畴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門贰镣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人膳凝,你說我怎么就攤上這事碑隆。” “怎么了蹬音?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵上煤,是天一觀的道長。 經(jīng)常有香客問我著淆,道長劫狠,這世上最難降的妖魔是什么拴疤? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮嘉熊,結(jié)果婚禮上遥赚,老公的妹妹穿的比我還像新娘。我一直安慰自己阐肤,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布讲坎。 她就那樣靜靜地躺著孕惜,像睡著了一般。 火紅的嫁衣襯著肌膚如雪晨炕。 梳的紋絲不亂的頭發(fā)上衫画,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天,我揣著相機與錄音瓮栗,去河邊找鬼削罩。 笑死,一個胖子當(dāng)著我的面吹牛费奸,可吹牛的內(nèi)容都是我干的弥激。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼愿阐,長吁一口氣:“原來是場噩夢啊……” “哼微服!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起缨历,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤以蕴,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后辛孵,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體丛肮,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年魄缚,在試婚紗的時候發(fā)現(xiàn)自己被綠了宝与。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡鲜滩,死狀恐怖伴鳖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情徙硅,我是刑警寧澤榜聂,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站嗓蘑,受9級特大地震影響须肆,放射性物質(zhì)發(fā)生泄漏匿乃。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一豌汇、第九天 我趴在偏房一處隱蔽的房頂上張望幢炸。 院中可真熱鬧,春花似錦拒贱、人聲如沸宛徊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽闸天。三九已至,卻和暖如春斜做,著一層夾襖步出監(jiān)牢的瞬間苞氮,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工瓤逼, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留笼吟,地道東北人。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓霸旗,卻偏偏與公主長得像贷帮,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子定硝,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容