-
type Animal struct {
? ?Colour string
}
type Student struct {
? ?Animal
? ?Name string
? ?Age ?int
}func (d *Student) Run() {
? ?fmt.Println(d.Name, "is running")
}
func (d *Animal) Eat() {
? ?fmt.Println(d.Colour, "is eating")
}查看全部 -
package struct_demo
import "fmt"
type Student struct {
? ?Name string
? ?Age ?int
}
func TestStudent() {
? ?// 方式一
? ?student := Student{
? ? ? Name: "張三",
? ? ? Age: ?18,
? ?}
? ?fmt.Println(student)
? ?// 方式二
? ?student2 := Student{}
? ?student2.Name = "李四"
? ?student2.Age = 20
? ?fmt.Println(student2)
? ?// 方式三
? ?var student3 Student
? ?student3.Name = "王五"
? ?student3.Age = 22
? ?fmt.Println(student3)
? ?// 方式四
? ?newStudent := new(Student) //區(qū)別:new() 函數(shù)返回的是指向結(jié)構(gòu)體的指針
? ?newStudent.Name = "趙六"
? ?newStudent.Age = 24
? ?fmt.Println(newStudent)
}查看全部 -
make :?
返回: 引用類(lèi)型
切片,map,chan
new:
返回:指針類(lèi)型
函數(shù)參數(shù) Type 用于創(chuàng)建變量的數(shù)據(jù)類(lèi)型
-----------------------
添加:?append()
刪除: delete()
拷貝 :copy()
----------------------
錯(cuò)誤和異常:
defer func() {
? ?message := recover()
? ?switch message.(type) {
? ?case string:
? ? ? fmt.Println("type:", reflect.TypeOf(message), "panic message:", message)
? ?case error:
? ? ? fmt.Println("type:", reflect.TypeOf(message), "panic error:", message)
? ?}
}()
//panic("test panic")
panic(errors.New("I am an error"))查看全部 -
func?getLen()?{ ????mIDSliceDst?:=?make([]string,2,5)?//容量5個(gè)只是指無(wú)需操作內(nèi)存了,而不是指能創(chuàng)建五個(gè)元素 ????mIDSliceDst[0]?=?"id1" ????mIDSliceDst[1]?=?"id2" ???? ???? ????fmt.Println(len(mIDSliceDst))?//長(zhǎng)度 ????fmt.Println(cap(mIDSliceDst))?//容量 } func?(){ ???mChan?:=?make(chan?int,1)? ???mChan?<-?1?????//?往chan里面寫(xiě)數(shù)據(jù) ???close(mChan)??//?關(guān)閉后就不可以往chan里面寫(xiě)數(shù)據(jù)了,起到限制作用,一般項(xiàng)目里面會(huì)加個(gè)defer ??? }查看全部 -
defer 執(zhí)行完成所有程序后再執(zhí)行defer后的程序
查看全部 -
message:=reocover()
查看捕獲到的錯(cuò)誤類(lèi)型
message.(type)
查看全部 -
panic 拋出異常
recover 捕獲異常
查看全部 -
delete只能刪除“鍵值”,所以只適用于Map
查看全部 -
new()出來(lái)的,是指針類(lèi)型
make()出來(lái)的,是引用類(lèi)型
查看全部 -
fmt.println(reflect.TypeOf())
查看類(lèi)型
查看全部 -
map[關(guān)鍵詞類(lèi)型]值類(lèi)型
比如:
makeMap:=make(map[int]string)
makeMap["10"]="dog"
查看全部 -
main.go是go語(yǔ)言的一個(gè)入口文件
查看全部 -
make就是內(nèi)建方法,類(lèi)似創(chuàng)建內(nèi)存
查看全部 -
內(nèi)建方法-創(chuàng)建json數(shù)據(jù)或者數(shù)據(jù)對(duì)
1.切片數(shù)組
func?makeSlice(){ ????mSlice?:=make([]string,3) ????mSlice[0]="dog" ????mSlice[0]="cat" ????mSlice[0]="tiger" ????fmt.Println(mSlice) }
2.創(chuàng)建數(shù)據(jù)對(duì)map
func?makeMap()??{ ????mMap?:=make(map[string]string) ????mMap["name"]="大帥哥" ????mMap["age"]="12" ????mMap["lastname"]="小帥哥" ????fmt.Println() ??? }
查看全部 -
2223
查看全部 -
接口
查看全部 -
make返回的是引用類(lèi)型
查看全部
舉報(bào)