Go基础学习四之函数function、结构struct、方法method
一、函数function1、基本概念函数是基本的代码块,用于执行一个任务。 2、函数定义函数定义格式如下: func function_name( [parameter list] ) [return_types] { 函数体 } 函数定义解析:
示例: /* 函数返回两个数的最大值 */ func max(num1,num2 int) int { /* 声明局部变量 */ var result int if (num1 > num2) { result = num1 } else { result = num2 } return result } 3、函数特性
ackage main import "fmt" func main() { A(1,2,3,4,5) } // ... 不定长变参 func A(a ...int) { fmt.Println(a) } 输出: ? myfirstgo go run func.go [1 2 3 4 5] 不定长变参特性: 4、匿名函数func main() { // 将一个函数赋值一个变量,该变量是函数类型 a := func(){ fmt.Println("匿名函数") } // 调用 a() } 5、闭包/** * 闭包函数 * * 该闭包函数接收一个int型参数,其返回值是函数类型 * */ func closure(x int) func(int) int { fmt.Println("%pn",&x) return func (y int) int { fmt.Println("%pn",&x) fmt.Println(x) fmt.Println(y) return x + y } } func main() { f := closure(10); fmt.Println(f(1)) fmt.Println(f(2)) } 打印结果: ? myfirstgo go run func.go %p 0xc42000e228 %p 0xc42000e228 10 1 11 %p 0xc42000e228 10 2 12 ? myfirstgo 6、defer
简单的测试: func main() { fmt.Println("a") defer fmt.Println("b") defer fmt.Println("c") } 上边的执行会打印什么结果呢? 会打印:a b c 吗? myfirstgo go run func.go a c b 实际打印的结果为:a c b
使用闭包 func main() { for i := 0; i < 3; i++ { // defer 普通调用 // defer fmt.Println(i) // 打印 2 1 0 // 使用闭包,引用局部变量 defer func () { fmt.Println(i) }() } } 打印结果: ? myfirstgo go run func.go 3 3 3 panic 使用示例: func main() { A() B() C() } func A() { fmt.Println("FUNC A") } func B() { // 匿名函数,如果没有参数,则末尾需要使用括号 defer func() { if err := recover(); err != nil { fmt.Println("Recover is B") } }() panic("B panic") } func C() { fmt.Println("FUNC C") } 打印结果: ? myfirstgo go run func.go FUNC A Recover is B FUNC C ? myfirstgo 二、结构Struct1.基本概念Go 语言中数组可以存储同一类型的数据,但 2.结构体定义结构体定义需要使用 type struct_variable_type struct { member definition; member definition; ... member definition; } 一旦定义了结构体类型,它就能用于变量的声明,语法格式如下: variable_name := structure_variable_type {value1,value2...valuen} type 是定义名称,struct 是结构体类型,如同 结构体可以包含多种数据类型,数组只能是单一类型的数据集合。 访问结构体成员 package main import "fmt" type Books struct { title string author string subject string book_id int } func main() { var Book1 Books /* 声明 Book1 为 Books 类型 */ var Book2 Books /* 声明 Book2 为 Books 类型 */ /* book 1 描述 */ Book1.title = "Go 语言" Book1.author = "www.runoob.com" Book1.subject = "Go 语言教程" Book1.book_id = 6495407 /* book 2 描述 */ Book2.title = "Python 教程" Book2.author = "www.runoob.com" Book2.subject = "Python 语言教程" Book2.book_id = 6495700 /* 打印 Book1 信息 */ fmt.Printf( "Book 1 title : %sn",Book1.title) fmt.Printf( "Book 1 author : %sn",Book1.author) fmt.Printf( "Book 1 subject : %sn",Book1.subject) fmt.Printf( "Book 1 book_id : %dn",Book1.book_id) /* 打印 Book2 信息 */ fmt.Printf( "Book 2 title : %sn",Book2.title) fmt.Printf( "Book 2 author : %sn",Book2.author) fmt.Printf( "Book 2 subject : %sn",Book2.subject) fmt.Printf( "Book 2 book_id : %dn",Book2.book_id) } 以上实例执行运行结果为: Book 1 title : Go 语言 Book 1 author : www.runoob.com Book 1 subject : Go 语言教程 Book 1 book_id : 6495407 Book 2 title : Python 教程 Book 2 author : www.runoob.com Book 2 subject : Python 语言教程 Book 2 book_id : 6495700 3.struct特性
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |