string 转 map
string.(map[string]interface{})
0730
strconv 参考:Package strconv
包strconv实现了对基本数据类型的字符串表示的转换
- 数值转换
Atoi(string to int ) Itoa(int to String)
i, err := strconv.Atoi("-42")
s := strconv.Itoa(-42)
ParseBool, ParseFloat, ParseInt, and ParseUint convert strings to values:
b, err := strconv.ParseBool("true")
f, err := strconv.ParseFloat("3.1415", 64)
i, err := strconv.ParseInt("-42", 10, 64)
u, err := strconv.ParseUint("42", 10, 64)
FormatBool, FormatFloat, FormatInt, and FormatUint convert values to strings
s := strconv.FormatBool(true)
s := strconv.FormatFloat(3.1415, 'E', -1, 64)
s := strconv.FormatInt(-42, 16)
s := strconv.FormatUint(42, 16)
string Conversions
Quote and QuoteToASCII convert strings to quoted Go string literals. The latter guarantees that the result is an ASCII string, by escaping any non-ASCII Unicode with \u:
Quote 和 QuoteToASCII 将字符串转换为带引号的字符串文字。后者保证结果是一个 ASCII 字符串,用 \ u 转义任何非 ASCII 的 Unicode
q := strconv.Quote("Hello, 世界")
q := strconv.QuoteToASCII("Hello, 世界")
- 字符串转换
rune
官方定义
等同于int32,主要来区分字符串值和整数值
// rune is an alias for int32 and is equivalent to int32 in all ways. It is
// used, by convention, to distinguish character values from integer values.
type rune = int32
demo
func main() {
var str="hello 世界"
//得到字符串的字节长度 (12)
//golang 中string 底层是通过bytes 数组实现的,中文字符在unicode下占2个字节,在utf-8编码下占3个字节,而golang默认编码正好是utf-8
fmt.Println(len(str))
//得到字符串的长度 (8)
l:=len([]rune(str))
fmt.Println(l)
//go中的unicode/utf8包提供了用utf-8获取长度的方法 (8)
l2:=utf8.RuneCountInString(str)
fmt.Println(l2)
}
defer
##demo
package main
import "fmt"
func main() {
defer fmt.Println("world")
fmt.Println("hello")
}