package main
import "fmt"
//type hero struct { // name string // age int // power int //} //func test (m map[int]hero) { // stu := m[2] // stu.power = 80 // m[2] = stu // fmt.Printf("%p\n",m) //} //func test1 (h hero) { // h.power = 120 // fmt.Println(h) //} func main() { //m := make(map[int]hero) //m[1] = hero{"zhnag",1,1} //m[2] = hero{"zhn333ag",1,1} //h := hero{"ssdsd",1,2} //fmt.Println(h) //test1(h) //fmt.Println(h) //var i int //i = 100 //fmt.Printf("%d\n",i) //fmt.Printf("%p\n",i) //fmt.Printf("%p\n",&i) //fmt.Printf("%d\n",*&i) ////指针变量 //var p *int //p = &i //把变量i地址赋值给变量p //fmt.Println(p) //*p = 80 //fmt.Println(*p) //fmt.Printf("i=%d,p=%p",i,p) //注意 //1.默认值 nil //空指针 //var p *int //fmt.Println(p) //野指针 指针变量指向了一个未知的空间,会报错,程序不允许出现野指针 //var p *int //*p = 45 //fmt.Println(p) //var a int //var p *int //p = &a //*p = 78 //fmt.Println(p,*p) //new函数 //var p *int //p = new(int) //创建一个int类型的 空间 //*p = 57 //fmt.Println(*p) //自动推导类型 //q := new(int) //*q = 999 //fmt.Println(q,*q) //& 取地址 引用 //* 取值 解引用 //a := 10 //b := 20 ////普通变量是值传递 //swap(a,b) //fmt.Println(a,b) //swap1(&a,&b) //fmt.Println(a,b) //数组 //arr := [3]int{1,2,3} ////数组指针 //var p *[3]int ////指针和数组建立关系 //p = &arr //fmt.Println(p,*p) ////通过指针操作数组 //(*p)[0] = 123 ////*p[0] = 123 //p[1] = 345 //fmt.Println(p,*p) //arr := [5]int{1,2,3,4,5} //p1 := &arr //p2 := &arr[0] //fmt.Printf("%p\n",p1) //fmt.Printf("%p\n",p2) //fmt.Printf("%T\n",p1) //fmt.Printf("%T\n",p2) //a := [5]int{1,2,3,4,5} //swap(&a) //fmt.Println(a) //a := 10 //b := 20 //c := 30 ////指针数组 //var arr = [3]*int{&a,&b,&c} //a := [3]int{1,2,3} //var arr = [1]*[3]int{&a} //fmt.Println(arr) //fmt.Printf("%T\n",arr) //(*arr[0])[0] = 345 //fmt.Println(a) //切片和指针 //s := [5]int{1,2,3,4,5} //p := &s //fmt.Printf("%p\n",p) //fmt.Printf("%p\n",s) // //(*p)[1] = 345 ////*p[1] = 345 //fmt.Println(p) //a := 10 ////一级指针 //p := &a //fmt.Println(p) //fmt.Printf("%T\n",p) ////二级指针 //p2 := &p //fmt.Printf("%T\n",p2) //顺序初始化 //var s1 = Student{Person{ // 1, // "zhang", // 1}, // 1} //fmt.Println(s1) //s2 := Student{Person{1,"22",2},"11",2} ////fmt.Println(s2) ////s3 := Student{score:100} ////fmt.Println(s3) //s2.score = 90 //s2.Person.id = 100 //s2.Person = Person{1,"22",4} //fmt.Println(s2) //s2.name = "sdfsfdsdfsf" //fmt.Println(s2) //指针类型匿名字段 //var s Student1 //s.score = 100 //s.Person = new(Person) //s.id = 19 //fmt.Println(s.name,s,s.Person) //var c Cat //c.name = "小花" //c.show() //var d Dog //d.name = "小黑" //d.show() //stu := Student{"zhang",1,1} // //fmt.Println(stu) //stu1 :=Student{"li",3,3} //stu.print() //fmt.Println(stu1) //p := person10{1,"make",22} //p.PrintInfo1() //p.PrintInfo2() //fmt.Println(p.PrintInfo1) //fmt.Println(p.PrintInfo2) //fmt.Printf("%T\n",p.PrintInfo1) //fmt.Printf("%T\n",p.PrintInfo2) //方法值,隐式传递 //pfunc1 := p.PrintInfo1 //pfunc1() ////方法表达式 //pfunc2 := person10.PrintInfo1 //pfunc2(p) //pfunc3 := (*person10).PrintInfo2 //pfunc3(&p) //fmt.Printf("%T\n",pfunc2) //fmt.Printf("%T\n",pfunc3) //var h Humaner //stu := student11{"xiaoming",1} ////stu.sayHi() ////将对象信息赋值给接口类型变量 //h = &stu //h.sayHi() //var i interface{} //fmt.Println(i) //fmt.Printf("%T\n",i) //var i []interface{} //fmt.Printf("%T\n",i) //i = append(i,10,2.2,'a') //fmt.Println(i) //var i interface{} //i = 10.2 ////value, ok := element.(T) ////值, 值得判断 := 接口变量.(数据类型) //value, ok := i.(int) //if ok { // fmt.Println("int",value) //} else { // fmt.Println("not int") //} //result, err := test(10,10) //if err != nil { // fmt.Println("err= ", err) //} else { // fmt.Println(result) //} //fmt.Println("hello") //fmt.Println("hello") //fmt.Println("hello") //fmt.Println("hello") //panic("hello error") //fmt.Println("hello") //fmt.Println("hello") //fmt.Println("hello") //fmt.Println("hello") //defer fmt.Println("hello") //defer fmt.Println("hello1") //defer fmt.Println("hello2") //panic("sdsfdsd") //defer fmt.Println("hello3") //defer fmt.Println("hello4") //defer fmt.Println("hello5") //fmt.Println("wnag") testA() testB() testC() } func testA() { fmt.Println("testA") } func testB() { defer func() { recover() }() panic("error") panic("error") fmt.Println("testB") } func testC() { fmt.Println("testC") } func test(a, b int) (result int, err error) { err = nil if b == 0 { fmt.Println("err=", err) } else { result = a / b } return } //定义接口 一般以er为结尾,根据接口实现功能 type Humaner interface { //方法 sayHi() } type student11 struct { name string age int } func (s *student11)sayHi() { fmt.Printf("hi %s",s.name) } type person10 struct { id int name string age int } func (p person10) PrintInfo1() { fmt.Printf("%p,%v\n",&p,p) } func (p *person10) PrintInfo2() { fmt.Printf("%p,%v\n",p,*p) }
//type Student struct { // name string // age int // score int //} ////普通类型属性改动不影响 指针类型会影响 //func (s *Student)print() { // s.score = -9 // fmt.Println(*s) //} //type Cat struct { // name string // age int //} //type Dog struct { // name string // age int //} //func (c Cat)show() { // fmt.Printf("%s 喵喵喵\n",c.name) //} //func (d Dog)show() { // fmt.Printf("%v %v\n",d.name,"汪汪汪") //} //方法定义 //func (对象)方法 (参数列表) 返回值列表 { // 代码体 //} ////父类 /*type Person struct { id int name string age int } //子类 type Student struct { Person name string score int } type Student1 struct { *Person score int }*/ //func swap(p *[5]int) { // p[0] = 123 //} //func swap1(num1, num2 *int) { // *num1, *num2 = *num2, *num1 // fmt.Println(*num1, *num2) //} //func swap(num1, num2 int) { // num1, num2 = num2, num1 // fmt.Println(num1, num2) //}
|
请发表评论