字典和字符串用起來(lái)和Python很類似苫纤。但是GO多了字符串和其他類型的轉(zhuǎn)換。在轉(zhuǎn)換過(guò)程中如果失敗,用err進(jìn)行定義卷拘。因此有時(shí)候轉(zhuǎn)換的時(shí)候喊废,輸入是有2個(gè)參數(shù)的。
package main
import (
"fmt"
"strconv" //字符串類型轉(zhuǎn)換時(shí)導(dǎo)入
"strings" //字符串的string操作時(shí)導(dǎo)入
"unicode/utf8" //包含中文的切片操作時(shí)導(dǎo)入
)
func main(){
//字典:
var m1 map[string]int
m1 = make(map[string]int)
m1["a"] = 1
m1["b"] = 2
m1["c"] = 3
fmt.Println(m1)
m2 :=make (map[string]interface{},10)
m2["a"] = 1
m2["b"] = "love"
m2["c"] = 3
fmt.Println(m2)
fmt.Println(len(m2))
//輸出相應(yīng)key的value
fmt.Println(m2["b"])
//使用循環(huán)遍歷字典
for key,value := range m2 {
fmt.Println("key is", key, ";" ,"value is", value)
}
//遍歷的結(jié)果雖然一樣栗弟,但是順序不一樣污筷。這點(diǎn)和Python相同
//字典是無(wú)序的,因此不能用切片乍赫,不能用角標(biāo)索引瓣蛀。
//且滿足Key:Value的關(guān)系。一般是不定長(zhǎng)雷厂,若不知道value的類型惋增,可以使用interface{},不指定類型
//字符串
//可哈希,意味著字符串一旦定義改鲫,不能被改變诈皿。只能通過(guò)字節(jié)切片[]byte或者[]rune的方式進(jìn)行修改
//byte 等同于int8,常用來(lái)處理ascii字符
//rune 等同于int32,常用來(lái)處理unicode或utf-8字符
//''的類型是rune
//""的類型是string
s := "Hello"
b :=[]byte(s)
b[0]='h'
//使用"",則會(huì)報(bào)錯(cuò)cannot use "h" (type untyped string) as type byte in assignment
b[1]='E'
s = string(b)
fmt.Println(s)
fmt.Println(len(s))
fmt.Println((s[0])) //輸出為104钩杰,ASCII碼對(duì)應(yīng)的是h
ss := "Hello我哎"
bb :=[]rune(ss)
fmt.Println(bb) //[72 101 108 108 111 25105 21710]
bb[0]='h'
//使用"",則會(huì)報(bào)錯(cuò)cannot use "h" (type untyped string) as type byte in assignment
bb[1]='E'
ss = string(bb)
fmt.Println(ss)
fmt.Println(len([]rune(ss))) //7
fmt.Println((ss[0])) //輸出為104纫塌,ASCII碼對(duì)應(yīng)的是h
//包含有中文的切片操作。
//使用rune讲弄。中文一般對(duì)應(yīng)2個(gè)字符長(zhǎng)度措左。
str :="Go來(lái)吧"
fmt.Println(len(str)) //8
fmt.Println(utf8.RuneCountInString(str)) //將字符串轉(zhuǎn)換成rune //4
runeint32 := []rune(str)
for i := range runeint32{
fmt.Printf("%c\n",runeint32[i])
}
//輸出有亂碼,原因就是使用了byte 8位輸出
bi := []byte(str)
for i := range bi{
fmt.Printf("%c\n",bi[i]) //Go??¥??§
}
//字符串操作:
//和Python類似避除,有contains ,HasPrefix,HasSuffix,Replace, Split
//ToLower, ToUpper, Repeat, Count, Index,Join
// https://blog.csdn.net/nyist_zxp/article/details/109154160
sen := "TodaY is is is Friday"
//contains
fmt.Println(strings.Contains(sen,"day")) //true
//HasPrefix
fmt.Println(strings.HasPrefix(sen,"F")) //false
//HasSuffix
fmt.Println(strings.HasSuffix(sen,"y")) //true
//Replace
fmt.Println(strings.Replace(sen,"is","IS VERY",2)) //TodaY IS VERY IS VERY is Friday
//Split
fmt.Println(strings.Split(sen,"")) //[T o d a Y i s i s i s F r i d a y]
//ToLower變小寫怎披,ToUpper變大寫
fmt.Println(strings.ToLower(sen)) //today is is is friday
fmt.Println(strings.ToUpper(sen)) //TODAY IS IS IS FRIDAY
//Repeat,輸入0和-1時(shí)報(bào)錯(cuò)
fmt.Println(strings.Repeat(sen,2))
//Count
fmt.Println(strings.Count(sen,"a")) //2
fmt.Println(strings.Count(sen,"")) //22 返回sen+1 的個(gè)數(shù)
//Index
fmt.Println(strings.Index(sen,"i"))//6
//Join
sen2 :=[]string{"I","Love","you"}
fmt.Println(strings.Join(sen2,"-")) //I-Love-you
fmt.Println(strings.Join(sen2," ")) //I Love you
//字符串類型的轉(zhuǎn)換:strcov
//https://www.cnblogs.com/saryli/p/13279643.html
//字符串轉(zhuǎn)int:Atoi()
//int轉(zhuǎn)字符串: Itoa()
//ParseTP類函數(shù)將string轉(zhuǎn)換為TP類型:ParseBool()瓶摆、ParseFloat()凉逛、ParseInt()、ParseUint()群井。因?yàn)閟tring轉(zhuǎn)其它類型可能會(huì)失敗状飞,所以這些函數(shù)都有第二個(gè)返回值表示是否轉(zhuǎn)換成功
//FormatTP類函數(shù)將其它類型轉(zhuǎn)string:FormatBool()、FormatFloat()书斜、FormatInt()诬辈、FormatUint()
//AppendTP類函數(shù)用于將TP轉(zhuǎn)換成字符串后append到一個(gè)slice中:AppendBool()、AppendFloat()荐吉、AppendInt()焙糟、AppendUint()
//1.int轉(zhuǎn)換為字符串:Itoa()
fmt.Println("a"+strconv.Itoa(666)) //a666
//2.string轉(zhuǎn)換為int:Atoi()
//由于string可能無(wú)法轉(zhuǎn)換為int,所以這個(gè)函數(shù)有兩個(gè)返回值:第一個(gè)返回值是轉(zhuǎn)換成int的值样屠,第二個(gè)返回值判斷是否轉(zhuǎn)換成功
i,err := strconv.Atoi("a")
if err != nil {
fmt.Printf("convert failed")
}
fmt.Println(i)
//Parse類函數(shù)用于轉(zhuǎn)換字符串為給定類型的值:ParseBool()穿撮、ParseFloat()缺脉、ParseInt()、ParseUint()悦穿。
b2, err := strconv.ParseBool("love")
f2, err := strconv.ParseFloat("3.1415", 64)
i2, err := strconv.ParseInt("-42", 10, 64)
u2, err := strconv.ParseUint("42", 10, 64)
fmt.Println(b2,f2,i2,u2) //false 3.1415 -42 42
//bitSize參數(shù)表示轉(zhuǎn)換為什么位的int/uint攻礼,有效值為0、8咧党、16秘蛔、32、64傍衡。當(dāng)bitSize=0的時(shí)候深员,表示轉(zhuǎn)換為int或uint類型。
//例如bitSize=8表示轉(zhuǎn)換后的值的類型為int8或uint8
//base參數(shù)表示以什么進(jìn)制的方式去解析給定的字符串蛙埂,有效值為0倦畅、2-36。當(dāng)base=0的時(shí)候绣的,表示根據(jù)string的前綴來(lái)判斷以什么進(jìn)制去解析:
//0x開頭的以16進(jìn)制的方式去解析叠赐,0開頭的以8進(jìn)制方式去解析,其它的以10進(jìn)制方式解析屡江。
//將給定類型格式化為string類型:
//FormatBool()芭概、FormatFloat()、FormatInt()惩嘉、FormatUint()
b3 := strconv.FormatBool(true)
c3 := strconv.FormatFloat(3.1415, 'E', -1, 64)
d3 := strconv.FormatInt(-42, 10)
e3 := strconv.FormatUint(42, 10)
fmt.Println(b3,c3,d3,e3) //true 3.1415E+00 -42 42
//prec控制精度(排除指數(shù)部分):對(duì)'f'罢洲、'e'、'E'文黎,它表示小數(shù)點(diǎn)后的數(shù)字個(gè)數(shù)惹苗;對(duì)'g'、'G'耸峭,它控制總的數(shù)字個(gè)數(shù)桩蓉。
//如果prec 為-1,則代表使用最少數(shù)量的劳闹、但又必需的數(shù)字來(lái)表示f院究。
}
輸出結(jié)果
map[a:1 b:2 c:3]
map[a:1 b:love c:3]
3
love
key is a ; value is 1
key is b ; value is love
key is c ; value is 3
hEllo
5
104
[72 101 108 108 111 25105 21710]
hEllo我哎
7
104
8
4
G
o
來(lái)
吧
G
o
?
?
¥
?
?
§
true
false
true
TodaY IS VERY IS VERY is Friday
[T o d a Y i s i s i s F r i d a y]
today is is is friday
TODAY IS IS IS FRIDAY
TodaY is is is FridayTodaY is is is Friday
2
22
6
I-Love-you
I Love you
a666
convert failed0
false 3.1415 -42 42
true 3.1415E+00 -42 42