1. 程式人生 > >golang strconv 包使用

golang strconv 包使用

strconv 官方文件
        https://godoc.org/strconv
strconv 包實現了 基本資料型別的字串 的轉換。

匯入包
import "strconv"

數字轉換
Atoi - string到int
Itoa  - int到string

package main

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

func main() {
	i, err := strconv.Atoi("-42")
	fmt.Println(reflect.TypeOf(i).String(), i, err)
	s := strconv.Itoa(-42)
	fmt.Println(reflect.TypeOf(s).String(), s)
}

執行結果:
int -42 <nil>
string -42

Quote、QuoteRune、QuoteRuneToASCII、QuoteToASCII

package main

import (
	"fmt"
	"strconv"
)

func main() {

	s := strconv.Quote(`"Fran & Freddie's Diner	☺"`)
	fmt.Printf("%T, %v\n", s, s)

	s = strconv.QuoteRune('☺')
	fmt.Printf("%T, %v\n", s, s)

	s = strconv.QuoteRuneToASCII('☺')
	fmt.Printf("%T, %v\n", s, s)

	s = strconv.QuoteToASCII(`"Fran & Freddie's Diner	☺"`)
	fmt.Printf("%T, %v\n", s, s)
}

執行結果:
string, "\"Fran & Freddie's Diner\t☺\""
string, '☺'
string, '\u263a'
string, "\"Fran & Freddie's Diner\t\u263a\""

ParseBool, ParseFloat, ParseInt, and ParseUint 轉化字串到值

package main

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

func main() {

	b, err := strconv.ParseBool("true")
	fmt.Println(reflect.TypeOf(b).String(), b, err)
	f, err := strconv.ParseFloat("3.1415", 64)
	fmt.Println(reflect.TypeOf(f).String(), f, err)
	i, err := strconv.ParseInt("-42", 10, 64)
	fmt.Println(reflect.TypeOf(i).String(), i, err)
	u, err := strconv.ParseUint("42", 10, 64)
	fmt.Println(reflect.TypeOf(u).String(), u, err)
}

執行結果:
bool true <nil>
float64 3.1415 <nil>
int64 -42 <nil>
uint64 42 <nil>

FormatBool, FormatFloat, FormatInt, and FormatUint 轉化值到字串

package main

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

func main() {

	s := strconv.FormatBool(true)
	fmt.Println(reflect.TypeOf(s).String(), s)
	s = strconv.FormatFloat(3.1415, 'E', -1, 64)
	fmt.Println(reflect.TypeOf(s).String(), s)
	s = strconv.FormatInt(-42, 16)
	fmt.Println(reflect.TypeOf(s).String(), s)
	s = strconv.FormatUint(42, 16) // 42 = 0x2A
	fmt.Println(reflect.TypeOf(s).String(), s)
}

執行結果:
string true
string 3.1415E+00
string -2a
string 2a

AppendBool, AppendFloat, AppendInt, and AppendUint 格式化值到切片

package main

import (
	"fmt"
	"strconv"
)

func main() {

	// 將 true 追加到 b
	b := []byte("bool:")
	b = strconv.AppendBool(b, true)
	fmt.Println(b)
	fmt.Println(string(b))

	b32 := []byte("float32:")
	b32 = strconv.AppendFloat(b32, 3.1415926535, 'E', -1, 32)
	fmt.Println(string(b32))

	b64 := []byte("float64:")
	b64 = strconv.AppendFloat(b64, 3.1415926535, 'E', -1, 64)
	fmt.Println(string(b64))

	b10 := []byte("int (base 10):")
	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))
}

執行結果:
[98 111 111 108 58 116 114 117 101]
bool:true
float32:3.1415927E+00
float64:3.1415926535E+00
int (base 10):-42
int (base 16):-2a

AppendQuote, AppendQuoteRune, AppendQuoteRuneToASCII, and AppendUint 格式化值到切片

package main

import (
	"fmt"
	"strconv"
)

func main() {

	b := []byte("quote:")
	b = strconv.AppendQuote(b, `"Fran & Freddie's Diner"`)
	fmt.Println(string(b))
}

執行結果:
quote:"\"Fran & Freddie's Diner\""

package main

import (
	"fmt"
	"strconv"
)

func main() {

	// 符文
	b := []byte("rune:")
	b = strconv.AppendQuoteRune(b, '☺')
	fmt.Println(string(b))
}

執行結果:
rune:'☺'

package main

import (
	"fmt"
	"strconv"
)

func main() {

	b := []byte("rune (ascii):")
	b = strconv.AppendQuoteRuneToASCII(b, '☺')
	fmt.Println(string(b))
}

執行結果:
rune (ascii):'\u263a'

package main

import (
	"fmt"
	"strconv"
)

func main() {

	b10 := []byte("uint (base 10):")
	b10 = strconv.AppendUint(b10, 42, 10)
	fmt.Println(string(b10))

	b16 := []byte("uint (base 16):")
	b16 = strconv.AppendUint(b16, 42, 16)
	fmt.Println(string(b16))
}

執行結果:
uint (base 10):42
uint (base 16):2a