以下是记录我学习Go时练习的小程序段,接上篇,已经过编译通过,在此记录,以供翻阅复习
//访问结构体成员 需要使用点符号.符号,格式为:
//结构体.成员名
package main
import "fmt"
type Books struct{
title string
author string
subject string
book_id int
}
func main(){
var Book1 Books
var Book2 Books
Book1.title="Go语言"
Book1.author="www.aaa.com"
Book1.subject="Go语言"
Book1.book_id=6495700
Book2.title="Go语言"
Book2.author="www.aaa.com"
Book2.subject="Go语言"
Book2.book_id=6495700
fmt.Printf( "Book 1 title : %s\n", Book1.title)
fmt.Printf( "Book 1 author : %s\n", Book1.author)
fmt.Printf( "Book 1 subject : %s\n", Book1.subject)
fmt.Printf( "Book 1 book_id : %d\n", Book1.book_id)
fmt.Printf( "Book 2 title : %s\n", Book2.title)
fmt.Printf( "Book 2 author : %s\n", Book2.author)
fmt.Printf( "Book 2 subject : %s\n", Book2.subject)
fmt.Printf( "Book 2 book_id : %d\n", Book2.book_id)
}
/*
Book 1 title : Go语言
Book 1 author : www.aaa.com
Book 1 subject : Go语言
Book 1 book_id : 6495700
Book 2 title : Go语言
Book 2 author : www.aaa.com
Book 2 subject : Go语言
Book 2 book_id : 6495700
*/
//结构体作为函数参数
package main
import "fmt"
type Books struct{
title string
author string
subject string
book_id int
}
func PrintBook(book Books){
fmt.Printf( "Book title : %s\n", book.title);
fmt.Printf( "Book author : %s\n", book.author);
fmt.Printf( "Book subject : %s\n", book.subject);
fmt.Printf( "Book book_id : %d\n", book.book_id);
}
func main(){
var Book1 Books
var Book2 Books
Book1.title="Go语言"
Book1.author="www.aaa.com"
Book1.subject="Go语言"
Book1.book_id=6495700
Book2.title="Go语言"
Book2.author="www.aaa.com"
Book2.subject="Go语言"
Book2.book_id=6495700
//打印Book信息
PrintBook(Book1)
PrintBook(Book2)
}
/*
Book title : Go语言
Book author : www.aaa.com
Book subject : Go语言
Book book_id : 6495700
Book title : Go语言
Book author : www.aaa.com
Book subject : Go语言
Book book_id : 6495700
*/
//结构体指针
//可以定义指向结构体的指针
/*
var struct_pointer *Books
struct_pointer=&Book1
使用结构体指针访问结构体成员,使用 "." 操作符:
struct_pointer.title;
*/
package main
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func PrintBook(book *Books){
fmt.Printf("Book title:%s\n",book.title);
fmt.Printf( "Book author : %s\n", book.author);
fmt.Printf( "Book subject : %s\n", book.subject);
fmt.Printf( "Book book_id : %d\n", book.book_id);
}
func main(){
var Book1 Books
var Book2 Books
Book1.title="Go语言"
Book1.author="www.aaa.com"
Book1.subject="Go语言"
Book1.book_id=6495700
Book2.title="Go语言"
Book2.author="www.aaa.com"
Book2.subject="Go语言"
Book2.book_id=6495700
PrintBook( &Book1)
PrintBook( &Book2)
}
/*
Book title:Go语言
Book author : www.aaa.com
Book subject : Go语言
Book book_id : 6495700
Book title:Go语言
Book author : www.aaa.com
Book subject : Go语言
Book book_id : 6495700
*/
//Go 切片Slice
/*
切片是对数组的抽象
Go数组长度不可改变
Go提供这种灵活功能强悍的内置类型切片(动态数组),长度不固定,可以追加元素,
在追加时可能使切片的容量增大
*/
/*
----定义切片
1.可声明一个未指定大小的数组来定义切片
var identifier []type
切片不需要说明长度
2.或者使用make()函数来创建切片
var slice1 []type=make([]type,len)
或者简写为:
slice1 :=make([]type,len)
也可以指定容量,其中capacity为可选参数
make([]type,len,capacity)
其中len是数组的长度,并且也是切片的初始长度
----切片初始化
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的切片
*/
/*
切片是可索引的,并且可以由 len()方法获取长度
切片中的容量计算方法 cap()可以测量切片最长可以到达多少
*/
package main
import "fmt"
func PrintSlice(x []int){
fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x) //注意%V
}
func main(){
var numbers=make([]int,3,5)
PrintSlice(numbers)
//运行结果:len=3 cap=5 slice=[0 0 0]
//空切片
var numbers1 []int
PrintSlice(numbers1)
if(numbers1==nil){
fmt.Printf("切片是空的")
}
/*运行结果:
len=0 cap=0 slice=[]
切片是空的
*/
}
//切片截取
//通过设置上下限来设置截取切片【lower-bound:upper-bound】
package main
import "fmt"
func PrintSlice(x []int){
fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}
func main(){
//创建切片
numbers:=[]int{0,1,2,3,4,5,6,7,8}
PrintSlice(numbers)
// 打印原始切片
fmt.Println("numbers ==",numbers)
// 打印子切片从索引1(包含) 到索引4(不包含)
fmt.Println("number[1:4] ==",numbers[1:4])
//默认下限为 0
fmt.Println("numbers[ :3] ==",numbers[ :3])
//默认上限为 len(s)
fmt.Println("numbers[4: ]==",numbers[4: ])
numbers1 :=make([]int,0,5)
PrintSlice(numbers1)
//打印子切片从索引 0(包含) 到索引 2(不包含)
number2 :=numbers[ :2]
PrintSlice(number2)
//打印子切片从索引 2(包含) 到索引 5(不包含)
number3 :=numbers[2:5]
PrintSlice(number3)
}
/*
len=9 cap=9 slice=[0 1 2 3 4 5 6 7 8]
numbers == [0 1 2 3 4 5 6 7 8]
number[1:4] == [1 2 3]
numbers[ :3] == [0 1 2]
numbers[4: ]== [4 5 6 7 8]
len=0 cap=5 slice=[]
len=2 cap=9 slice=[0 1]
len=3 cap=7 slice=[2 3 4]
*/
//append() 向切片追加新元素 ||从切片拷贝 copy()函数
//如果想增加切片的容量,我们必须创建一个新的更大的切片并把原切片的内容都拷贝过来
package main
import "fmt"
func PrintSilce(x []int){
fmt.Printf("len =%d cap= %d slice= %v \n",len(x),cap(x),x)
}
func main(){
var numbers []int
PrintSilce(numbers)
//允许追加空切片
numbers=append(numbers,0)
PrintSilce(numbers)
//向切片中添加一个元素
numbers=append(numbers,1)
PrintSilce(numbers)
//同时添加多个元素
numbers=append(numbers,2,3,4)
PrintSilce(numbers)
//创建切片numbers1是之前切片的两倍容量
numbers1:=make([]int,len(numbers),(cap(numbers))*2)
//拷贝numbers的内容到numbers1
copy(numbers1,numbers)
PrintSilce(numbers1)
/*
len=9 cap=9 slice=[0 1 2 3 4 5 6 7 8]
numbers == [0 1 2 3 4 5 6 7 8]
number[1:4] == [1 2 3]
numbers[ :3] == [0 1 2]
numbers[4: ]== [4 5 6 7 8]
len=0 cap=5 slice=[]
len=2 cap=9 slice=[0 1]
len=3 cap=7 slice=[2 3 4]
*/
}
//GO语言范围(Range)
/*Go语言中range关键字用于for循环中迭代数组(array),切片(slice),通道(channel)或集合(map)的元素。
在数组和切片中它返回元素的索引和索引对应的值,在集合中返回Key-value对的key值
*/
package main
import "fmt"
func main(){
//这是我们使用range去求一个slice的和,使用数组和这个很相似
nums:=[]int{2,3,4}
sum:=0
//在数组上使用range 将传入index和值 两个变量。
//我们不需要使用该元素的序号,所以我们使用空白符"_"省略了。
for _,num:=range nums{
sum+=num
}
fmt.Println("sum: ",sum)
//要显示索引的时候
for index,num:=range nums{
if num==3{
fmt.Println("数值3对应的index: ",index)
}
}
//range也可以用在map的键值对上。
kvs :=map[string] string{"a":"apple","b":"banana"}
for key,value :=range kvs{
fmt.Printf("%s->%s\n",key,value)
}
//range也可以用来枚举Unicode字符串,
for i,c :=range "go"{
fmt.Println(i,c)
}
/*
运行结果:
sum: 9
数值3对应的index: 1
a->apple
b->banana
0 103
1 111
*/
}