基本的main.go
package main
import "fmt"
func main() {
var helloStr string
helloStr = "Hello World!"
helloStr2 := helloStr
fmt.Println(helloStr)
fmt.Println(helloStr2)
}
# 輸出
Hello World!
Hello World!
內(nèi)置類型
布爾 :
bool
-
整型 :
int int8 int16 int32 int64
-
int
根據(jù)操作系統(tǒng)決定是int32
還是int64
-
-
無符號整型 :
uint uint8 uint16 uint32 uint64 uintptr
-
uint
根據(jù)操作系統(tǒng)決定是uint32
還是uint64
-
uintptr
是存儲指針的uint32
或uint64
整數(shù)
-
-
整型變體 :
-
byte
: uint8 的別名 -
rune
: int32的別名,代表一個 Unicode碼
-
指針 :
初始化:var aPtr *int = &aInt
初始化:var aPtr *int = new(int)
浮點 :
float32
float64
復數(shù) :
complex64
complex128
字符串 :
string
數(shù)組(固定長度的數(shù)組) : 使用
[5]int
表示
初始化:aArray := [...]int{1, 2, 3, 4, 5, 6}
切片(長度不固定) : 使用
[]int
表示
初始化:aSlice := []int{}
初始化:aSlice := make([]int, 0, 0)
映射 : 使用
map[string]int
表示
初始化:aMap := map[string]int{}
初始化:aMap := make(map[string]int, 8)
-
管道 : 使用
chan int
表示- 初始化:
aChannel := make(chan int)
- 只讀單向管道使用
<-chan int
表示 - 只寫單向管道使用
chan<- int
表示
- 初始化:
-
結構體struct :
type Student struct {}
- 其實是使用了
type {NewTypeName} {OldTypeName}
語法, 創(chuàng)建自定義類型, 例如type MyInt int
- 如果是
type MyInt = int
則其實MyInt
和int
等價, 這被稱為類型別名
- 其實是使用了
函數(shù)function :
error接口:
error
, 任何實現(xiàn)了Error() String
方法的對象都可以認為是error
類型
error接口
type error interface {
Error() String
}
示例代碼
package main
import "fmt"
func main() {
aArray1 := [...]int{1, 2, 3, 4, 5, 6}
aArray2 := [5]int{1, 2, 3, 4}
fmt.Println("aArray1 is ", aArray1)
fmt.Println("aArray2 is ", aArray2)
fmt.Println("===============")
aSlice1 := []int{}
aSlice2 := make([]int, 10, 10)
fmt.Println("aSlice1 is ", aSlice1)
fmt.Println("aSlice2 is ", aSlice2)
fmt.Println("===============")
aMap1 := map[int]int{}
aMap2 := make(map[int]int, 8)
fmt.Println("aMap1 is ", aMap1)
fmt.Println("aMap2 is ", aMap2)
fmt.Println("===============")
var aChan1 chan int = make(chan int)
aChan2 := make(<-chan int)
aChan3 := make(chan<- int)
fmt.Println("aChan1 is ", aChan1)
fmt.Println("aChan2 is ", aChan2)
fmt.Println("aChan3 is ", aChan3)
fmt.Println("===============")
type Student struct {
age int
}
aStu := Student{}
fmt.Println("aStu is ", aStu)
fmt.Println("===============")
aFunc := func(a int, b int) int {
return a + b
}
fmt.Println("aFunc is ", aFunc)
fmt.Println("===============")
var aInterface interface{}
aInterface=aStu
fmt.Println("aInterface is ", aInterface)
}
# 輸出
aArray1 is [1 2 3 4 5 6]
aArray2 is [1 2 3 4 0]
===============
aSlice1 is []
aSlice2 is [0 0 0 0 0 0 0 0 0 0]
===============
aMap1 is map[]
aMap2 is map[]
===============
aChan1 is 0xc000052060
aChan2 is 0xc0000520c0
aChan3 is 0xc000052120
===============
aStu is {0}
===============
aFunc is 0x47cde0
===============
aInterface is {0}
內(nèi)置函數(shù)
初始化變量的函數(shù)
-
make
: 僅用于初始化slice
,map
,chan
-
new
: 僅用于初始化除slice
,map
,chan
之外的其他類型的指針類型 - 除
slice
,map
,chan
之外的其他類型聲明即賦予相對于其類型的零值, 例如var aBool bool
,aBool
被賦予false
示例代碼
package main
import "fmt"
func main() {
var aPointer *int
aPointer = new(int)
fmt.Println("aPointer is ", aPointer)
fmt.Println("===============")
var aSlice []int
aSlice = make([]int, 10, 10)
fmt.Println("aSlice is ", aSlice)
fmt.Println("===============")
var aMap map[int]int
aMap = make(map[int]int, 8)
fmt.Println("aMap is ", aMap)
fmt.Println("===============")
var aChan chan int
aChan = make(chan int)
fmt.Println("aChan is ", aChan)
}
# 輸出
aPointer is 0xc0000aa058
===============
aSlice is [0 0 0 0 0 0 0 0 0 0]
===============
aMap is map[]
===============
aChan is 0xc000086060
關于數(shù)組切片的函數(shù)
append
: 向切片中追加元素 ,aSlice = append(aSlice, 1)
len
: 獲取數(shù)組或切片的長度 ,len(aSlice)
cap
: 獲取數(shù)組或切片的容量 ,cap(aSlice)
遍歷 :
for index,value := range aSlice {
-
切片截取 :
-
aSlice[low:high:max]
得到的結果的len=high-low
,cap=max-low
, high和max的值不能大于 原切片的capacity aSlice[low:high]
aSlice[low:]
-
aSlice[:high]
或aSlice[:high:max]
-
關于復制的函數(shù)copy :
copy
:copy(dst,src)
根據(jù)src
的len
和dst
的len
拷貝內(nèi)容 , 例如n4 := copy(s[3:], a[5:])
將a
數(shù)組中第6個元素至最后一個元素 依次拷貝到s
切片中第4個元素位置,直到拷貝完成或到達s
切片的len
限制
示例代碼
package main
import "fmt"
func main() {
var aSlice []int
aSlice = make([]int, 0, 1)
fmt.Println("cap(aSlice) is ", cap(aSlice))
aSlice = append(aSlice, 1)
aSlice = append(aSlice, 2)
aSlice = append(aSlice, 3)
fmt.Println("aSlice is ", aSlice)
fmt.Println("len(aSlice) is ", len(aSlice))
fmt.Println("cap(aSlice) is ", cap(aSlice))
fmt.Println("=======遍歷示例=====")
for i, v := range aSlice {
fmt.Println("index:", i, "value:", v)
}
fmt.Println("============")
aSlice2:=aSlice[1:4:4]
fmt.Println("aSlice2 is ", aSlice2)
fmt.Println("len(aSlice2) is ", len(aSlice2))
fmt.Println("cap(aSlice2) is ", cap(aSlice2))
fmt.Println("============")
aSlice3:=make([]int,1,4)
copy(aSlice3[0:],aSlice)
fmt.Println("aSlice3 is ", aSlice3)
fmt.Println("len(aSlice3) is ", len(aSlice3))
fmt.Println("cap(aSlice3) is ", cap(aSlice3))
}
# 輸出
cap(aSlice) is 1
aSlice is [1 2 3]
len(aSlice) is 3
cap(aSlice) is 4
=======遍歷示例=====
index: 0 value: 1
index: 1 value: 2
index: 2 value: 3
============
aSlice2 is [2 3 0]
len(aSlice2) is 3
cap(aSlice2) is 3
============
aSlice3 is [1]
len(aSlice3) is 1
cap(aSlice3) is 4
關于映射的函數(shù)
- 從map中刪除key:
delete(scene, "brazil")
- delete可以刪除不存在的key
- delete函數(shù)不返回任何值
- 獲取key對應的值:
value := map[key]
不會失敗,如果key不存在,則返回類型對應的零值 - 判斷鍵是否存在 :
value, ok := map[key]
- 遍歷 :
for key,value := range aMap {
- 只遍歷key :
for key := range aMap {
package main
import "fmt"
func main() {
var aMap map[string]int
aMap = make(map[string]int, 8)
fmt.Println("aMap is ", aMap)
fmt.Println("len(aMap) is ", len(aMap))
aMap["張三"] = 90
aMap["小明"] = 100
fmt.Println("============")
fmt.Println("aMap is ", aMap)
fmt.Println("len(aMap) is ", len(aMap))
fmt.Println("aMap[\"張三\"] is ", aMap["張三"])
fmt.Println("aMap[\"李四\"] is ", aMap["李四"]) //獲取不存在的key,返回類型對應的零值
if value, ok := aMap["李四"]; ok {
fmt.Println("aMap[\"李四\"] is ", value)
} else {
fmt.Println("aMap[\"李四\"] is not exists ")
}
fmt.Println("=======遍歷示例1=====")
for k, v := range aMap {
fmt.Println("key:", k, "value:", v)
}
fmt.Println("=======遍歷示例2=====")
for k := range aMap {
fmt.Println("key:", k, "value:", aMap[k])
}
fmt.Println("============")
delete(aMap, "張三")
fmt.Println("aMap is ", aMap)
fmt.Println("len(aMap) is ", len(aMap))
delete(aMap, "張三") // 可以刪除不存在的key
}
# 輸出
aMap is map[]
len(aMap) is 0
============
aMap is map[小明:100 張三:90]
len(aMap) is 2
aMap["張三"] is 90
aMap["李四"] is 0
aMap["李四"] is not exists
=======遍歷示例1=====
key: 張三 value: 90
key: 小明 value: 100
=======遍歷示例2=====
key: 張三 value: 90
key: 小明 value: 100
============
aMap is map[小明:100]
len(aMap) is 1
關于管道的函數(shù)
- 關閉 chan :
close(aChan)
關于異趁诓危恢復的函數(shù)
-
panic
: -
recover
:
關于字符串的函數(shù)
- 獲取字符串長度(bytes長度):
len(str)
- 獲取字符串長度(字符長度,含中文):
len([]rune(str))
- 遍歷字符串:
for _, r := range s { //rune
- 拼接字符串 : 使用
+
或fmt.Sprintf
- 分割字符串: 使用
strings.Split
- join字符串:
strings.Join(a[]string, sep string)
- 判斷是否包含:
strings.Contains
- 前綴/后綴判斷 :
strings.HasPrefix
,strings.HasSuffix
- 子串出現(xiàn)的位置:
strings.Index()
,strings.LastIndex()
package main
import (
"fmt"
"strings"
)
func main() {
var aStr string
aStr="Hello世界!"
fmt.Println("len(aStr) is ", len(aStr))
fmt.Println("len(aStr) is ", len([]rune(aStr)))
fmt.Println("=====遍歷字符串=======")
for i,v := range aStr { //rune
fmt.Println(i,string(v))
}
fmt.Println("============")
aStr2:=aStr+"拼接字符串"
fmt.Println("aStr2 is ", aStr2)
aStr3:=fmt.Sprintf("%s拼接字符串",aStr)
fmt.Println("aStr3 is ", aStr3)
fmt.Println("============")
aSlice:=strings.Split(aStr,"o")
fmt.Println("strings.Split(aStr,\"o\") is ", aSlice)
aStr4:=strings.Join(aSlice,"...")
fmt.Println("strings.Join(aSlice,\"...\") is ", aStr4)
fmt.Println("============")
fmt.Println("strings.ContainsAny(aStr,\"ll\") is ", strings.ContainsAny(aStr,"ll"))
fmt.Println("strings.HasPrefix(aStr,\"He\") is ", strings.HasPrefix(aStr,"He"))
fmt.Println("strings.HasSuffix(aStr,\"世界!\") is ", strings.HasSuffix(aStr,"世界!"))
fmt.Println("strings.Index(aStr,\"l\") is ", strings.Index(aStr,"l"))
fmt.Println("strings.LastIndex(aStr,\"l\") is ", strings.LastIndex(aStr,"l"))
}
# 輸出
len(aStr) is 12
len(aStr) is 8
=====遍歷字符串=======
0 H
1 e
2 l
3 l
4 o
5 世
8 界
11 !
============
aStr2 is Hello世界!拼接字符串
aStr3 is Hello世界!拼接字符串
============
strings.Split(aStr,"o") is [Hell 世界!]
strings.Join(aSlice,"...") is Hell...世界!
============
strings.ContainsAny(aStr,"ll") is true
strings.HasPrefix(aStr,"He") is true
strings.HasSuffix(aStr,"世界!") is true
strings.Index(aStr,"l") is 2
strings.LastIndex(aStr,"l") is 3
關于復數(shù)的函數(shù)
-
complex
: 生成復數(shù),var x complex128 = complex(1, 2) // 1+2i
-
real
: 獲取復數(shù)的實部real(x*y)
-
imag
: 獲取復數(shù)的虛部imag(x*y)
package main
import (
"fmt"
)
func main() {
var aComplex complex128
aComplex = complex(1, 2)
fmt.Println("aComplex is ", aComplex)
fmt.Println("real(aComplex) is ", real(aComplex))
fmt.Println("imag(aComplex) is ", imag(aComplex))
}
# 輸出
aComplex is (1+2i)
real(aComplex) is 1
imag(aComplex) is 2