struct 类似于 java 中的类,可以在 struct 中定义成员变量。
要访问成员变量,可以有两种方式:
不需要通过 getter, setter 来设置访问权限。
type Rect struct{ //定义矩形类 x,y float64 //类型只包含属性,并没有方法 width,height float64 } func (r *Rect) Area() float64{ //为Rect类型绑定Area的方法,*Rect为指针引用可以修改传入参数的值 return r.width*r.height //方法归属于类型,不归属于具体的对象,声明该类型的对象即可调用该类型的方法 }
结构体是作为参数的值传递:
package main import "fmt" type Books struct { title string author string subject string book_id int } func changeBook(book Books) { book.title = "book1_change" } func main() { var book1 Books; book1.title = "book1" book1.author = "zuozhe" book1.book_id = 1 changeBook(book1) fmt.Println(book1) }
结果为:
{book1 zuozhe 1}
如果想在函数里面改变结果体数据内容,需要传入指针:
package main import "fmt" type Books struct { title string author string subject string book_id int } func changeBook(book *Books) { book.title = "book1_change" } func main() { var book1 Books; book1.title = "book1" book1.author = "zuozhe" book1.book_id = 1 changeBook(&book1) fmt.Println(book1) }
{book1_change zuozhe 1}
测试实例:
package main import "fmt" func main() { var a int = 10 var ip *int fmt.Printf("变量的地址:%x\n", &a) fmt.Println("变量的地址:", &a) ip = &a fmt.Println("ip 变量存储的指针地址:", ip) fmt.Println("ip 变量存储的指针地址的值:", *ip) fmt.Println("ip 变量存储的指针地址的地址:", &ip) var ptr *int if (ptr != nil) { if (ip != nil) { fmt.Println("ptr不是空指针") fmt.Println("ip不是空指针") }else { fmt.Println("ptr不是空指针") fmt.Println("ip是空指针") } } else { if(ip != nil){ fmt.Println("ptr是空指针") fmt.Println("ip不是空指针") }else{ fmt.Println("ptr是空指针") fmt.Println("ip是空指针") } } /* 自学的时候想到能不能使用 switch 优化 for 繁琐的写法,但是发现 case 匹配到后会自动跳出 switch。 查了一下 select 等方法发现并不适用, 最后发现了 fallthrough 可以很好的用在这里(不过要注意 fallthrough 存在的位置,避免产生逻辑混乱) */ switch { case ptr != nil: fmt.Println("ptr不是空指针") fallthrough case ptr == nil: fmt.Println("ptr是空指针") fallthrough case ip != nil: fmt.Println("ip不是空指针") default: fmt.Println("ip是空指针") } }
以上代码执行结果为:
变量的地址:c420080008 变量的地址: 0xc420080008 ip 变量存储的指针地址: 0xc420080008 ip 变量存储的指针地址的值: 10 ip 变量存储的指针地址的地址: 0xc42008a018 ptr是空指针 ip不是空指针 ptr是空指针 ip不是空指针
数组初始化
初始化数组的初始化有多种形式。
[5] int {1,2,3,4,5}
长度为5的数组,其元素值依次为:1,2,3,4,5。
[5] int {1,2}
长度为 5 的数组,其元素值依次为:1,2,0,0,0 。
在初始化时没有指定初值的元素将会赋值为其元素类型 int 的默认值0,string 的默认值是 ""。
[...] int {1,2,3,4,5}
长度为 5 的数组,其长度是根据初始化时指定的元素个数决定的。
[5] int { 2:1,3:2,4:3}
长度为 5 的数组,key:value,其元素值依次为:0,0,1,2,3。在初始化时指定了 2,3,4 索引中对应的值:1,2,3
[...] int {2:1,4:3}
长度为5的数组,起元素值依次为:0,0,1,0,3。由于指定了最大索引 4 对应的值 3,根据初始化的元素个 数确定其长度为5赋值与使用。
切片的初始化
切片可以通过数组来初始化,也可以通过内置函数 make() 初始化。
初始化时 len=cap,在追加元素时如果容量 cap 不足时将按 len 的 2 倍扩容。
s :=[] int {1,2,3 } 直接初始化切片,[] 表示是切片类型,{1,2,3} 初始化值依次是 1,2,3。其 cap=len=3。
s := arr[:] 初始化切片 s,是数组 arr 的引用。
s := arr[startIndex:endIndex] 将 arr 中从下标 startIndex 到 endIndex-1 下的元素创建为一个新的切片。
s := arr[startIndex:] 缺省 endIndex 时将表示一直到 arr 的最后一个元素。
s := arr[:endIndex] 缺省 startIndex 时将表示从 arr 的第一个元素开始。
s1 := s[startIndex:endIndex] 通过切片 s 初始化切片 s1
s :=make([]int,len,cap) 通过内置函数 make() 初始化切片 s,[]int 标识为其元素类型为 int 的切片。
那我也来输出一个杨辉三角吧:
package main import "fmt" func main() { yanghuisanjiao(12) } func yanghuisanjiao(rows int) { for i := 0; i < rows; i++ { number := 1; for k := 0; k < rows-i; k++ { fmt.Print(" ") } for j := 0; j <= i; j++ { fmt.Printf("%5d", number) number = number * (i - j) / (j + 1); } fmt.Println() } }
打印结果:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 1 10 45 120 210 252 210 120 45 10 1 1 11 55 165 330 462 462 330 165 55 11 1
感谢您的支持,我会继续努力的!
支付宝扫一扫,即可进行扫码打赏哦
2150Go 语言结构体
struct 类似于 java 中的类,可以在 struct 中定义成员变量。
要访问成员变量,可以有两种方式:
不需要通过 getter, setter 来设置访问权限。
2149Go 语言结构体
结构体是作为参数的值传递:
结果为:
如果想在函数里面改变结果体数据内容,需要传入指针:
结果为:
2148Go 语言指针
测试实例:
以上代码执行结果为:
2147Go 语言数组
数组初始化
初始化数组的初始化有多种形式。
长度为5的数组,其元素值依次为:1,2,3,4,5。
长度为 5 的数组,其元素值依次为:1,2,0,0,0 。
在初始化时没有指定初值的元素将会赋值为其元素类型 int 的默认值0,string 的默认值是 ""。
长度为 5 的数组,其长度是根据初始化时指定的元素个数决定的。
长度为 5 的数组,key:value,其元素值依次为:0,0,1,2,3。在初始化时指定了 2,3,4 索引中对应的值:1,2,3
长度为5的数组,起元素值依次为:0,0,1,0,3。由于指定了最大索引 4 对应的值 3,根据初始化的元素个 数确定其长度为5赋值与使用。
切片的初始化
切片可以通过数组来初始化,也可以通过内置函数 make() 初始化。
初始化时 len=cap,在追加元素时如果容量 cap 不足时将按 len 的 2 倍扩容。
s :=[] int {1,2,3 } 直接初始化切片,[] 表示是切片类型,{1,2,3} 初始化值依次是 1,2,3。其 cap=len=3。
s := arr[:] 初始化切片 s,是数组 arr 的引用。
s := arr[startIndex:endIndex] 将 arr 中从下标 startIndex 到 endIndex-1 下的元素创建为一个新的切片。
s := arr[startIndex:] 缺省 endIndex 时将表示一直到 arr 的最后一个元素。
s := arr[:endIndex] 缺省 startIndex 时将表示从 arr 的第一个元素开始。
s1 := s[startIndex:endIndex] 通过切片 s 初始化切片 s1
s :=make([]int,len,cap) 通过内置函数 make() 初始化切片 s,[]int 标识为其元素类型为 int 的切片。
2146Go 语言数组
那我也来输出一个杨辉三角吧:
打印结果: