package main
import"fmt"import"02_type/model"funcmain(){
fmt.Println("==>> 定义map;通过make 进行初始化,分配内存空间:")var a map[int]string// 10 表示可用存放10个键值对
a =make(map[int]string,10)
fmt.Println("a=", a)
fmt.Println("\n==>> 几种定义map的方式:")
b :=make(map[int]string)
fmt.Println("b=", b,"b.len=",len(b))
b[1]="1"
b[2]="2"
fmt.Println("b=", b,"b.len=",len(b))
c :=map[int]string{1:"1",2:"2",// 右大括号换行时,这个逗号不能省略}
c[3]="3"
fmt.Println("c=", c)
fmt.Println("\n==>> map增删改查:")
fmt.Println("==>> 新增元素:")
a[1]="1"
a[222]="222"
a[3]="谁"
fmt.Println("a=", a)
fmt.Println("==>> map 修改元素:")
a[3]="go-java-c"
fmt.Println("a[3] 修改后,a=", a)
fmt.Println("==>> map 删除元素:")delete(a,3)
fmt.Println("a[3] 删除后,a=", a)delete(a,4)
fmt.Println("a[4](没有的key) 删除后,a=", a)
val,found := a[4]
fmt.Println("val=", val)
fmt.Println("found=", found)
fmt.Println("\n==>> 遍历c:", c)for k,v :=range c {
fmt.Printf("key:%v, val:%v\n", k, v)}
fmt.Println("\n==>> map 的val为map:")
d :=make(map[string]map[int]string)
fmt.Println(d)
d["1"]=make(map[int]string)
d["1"][2012500407]="407"
d["1"][2012500408]="408"
d["1"][2012500409]="409"
fmt.Println("d=", d)for key, val :=range d {
fmt.Println("key=", key)for stuNo, name :=range val {
fmt.Println("stuNo=", stuNo,"name=", name)}}
fmt.Println("\n ==>> 结构体:")
fmt.Println("\n ==>> ----------------几种不同的初始化,赋值方式-------------------")var s1 Stu
fmt.Println("s1=", s1)
s1.Name ="小脑斧"
s1.age =18
s1.School ="清华大学"
fmt.Println("字段赋值后:s1=", s1)
fmt.Println("age=", s1.age)
fmt.Println("==>> 声明时直接给属性赋值:")var stu Stu = Stu{"脑斧",18,"peking"}
fmt.Println("stu=", stu)
fmt.Println("==>> 使用指针赋值:")var s *Stu =new(Stu)// go 默认解地址
s.Name ="脑斧"(*s).age =18
fmt.Println("指针方式: s=",*s)
fmt.Println("==>> & 取地址:")var s2 *Stu =&Stu{}
s2.Name ="小脑斧"
s2.School ="清华大学"
fmt.Println("s2=",*s2)
fmt.Println("==>> & 取地址,并初始化属性:")var s3 =&Stu{"脑斧",18,"peking"}
fmt.Println("s3=",*s3)
fmt.Println("==.>> 两个结构体之间强制转换:")var stu2 Student
var p Person
stu2 =Student(p)
fmt.Println("stu2 = Student(p): stu2=",stu2)
fmt.Println("==>> 起别名:")var student Student = Student{10}var stuAlias StuAlias = StuAlias{20}// 报错: Cannot use 'stuAlias' (type StuAlias) as the type Student// student = stuAlias
student =Student(stuAlias)
fmt.Println("student=", student)
fmt.Println("stuAlias=", stuAlias)
fmt.Println("StuAlias-> Student: student=", student)
fmt.Println("\n==>> 方法:")
fmt.Println("==>> -----------------------------------")// 调用var aTest A = A{18}
aTest.aMethod()
fmt.Println("==>> 方法值传递测试:")
fmt.Println("aTest.Num=", aTest.Num)
aTest.numUpdate(20)
fmt.Println("aTest.Num 更新后,并未改变:aTest=", aTest)
fmt.Println("==>> 方法使用地址传递:")
fmt.Println("aTest.Num=", aTest.Num)(&aTest).numUpdatePointer(19)
fmt.Println("使用 &aTest调用 aTest.Num 更新后:aTest=", aTest)// go 默认会取地址传递地址;编译器进行了优化
aTest.numUpdatePointer(20)
fmt.Println("使用默认传地址: aTest.Num 更新后:aTest=", aTest)
fmt.Println("\n==>> 基础数据类型别名绑定方法:")var i integer =20
i.printInt()
fmt.Println("==>> 方法体内修改(i=18)后:")
i.changeInt()
fmt.Println("i=", i)
fmt.Println("\n==>> 结构体实现 String() 方法:")
human := Human{Name:"linux", Age:18}
fmt.Println("human=", human)
fmt.Println("使用 &human 可以调用 String() 方法:\n &human=",&human)
fmt.Println("\n==>> 跨包创建结构体测试:")
modelStu := model.Student{Name:"脑斧", Age:18}
fmt.Println("modelStu=", modelStu)
fmt.Println("==>> 跨包创建结构体, 首字母小写,使用方法暴露:")
lowCaseModelStu := model.GetLowCaseStudent("小脑斧",19)
fmt.Println("lowCaseModelStu=",*lowCaseModelStu)}type Human struct{
Name string
Age int}func(h *Human)String()string{
str := fmt.Sprintf("Name= %v, Age= %v", h.Name, h.Age)return str
}type integer intfunc(i integer)printInt(){
fmt.Println("i= ", i)}func(i *integer)changeInt(){*i =18
fmt.Println("i= ",*i)}type A struct{
Num int}func(a *A)numUpdatePointer(numNew int){
a.Num = numNew
}func(a A)aMethod(){
fmt.Println("A.num=",a.Num)}func(a A)numUpdate(numNew int){
a.Num = numNew
}type StuAlias Student
type Student struct{
Age int}type Person struct{
Age int}type Stu struct{
Name string
age int// 小写则包外不可访问
School string}
model/Student.go
package model
type Student struct{
Name string
Age int}type lowCaseStudent struct{
Name string
Age int}funcGetLowCaseStudent(name string, age int)*lowCaseStudent{return&lowCaseStudent{name, age}}