type 结构体名称 struct {
成员1 类型
成员2 类型
...
}
type XiShu struct {
ID int
Name string
Age int
Position string
}
package main
import "fmt"
type XiShu struct {
ID int
Name string
Age int
}
func main() {
var xiShu XiShu
fmt.Printf("结构体为:%+v",xiShu)
}
结构体为:{ID:0 Name: Age:0}
示例可见:声明结构体变量之后,结构体不为空,结构体每个成员都为默认值(字串默认值为空,int 默认值为 0)。
结构体名 = 结构体类型{ 成员1: 值1, 成员2: 值2,... }
package main
import "fmt"
type XiShu struct {
ID int
Name string
Age int
}
func main() {
xiShu := XiShu{
ID: 1,
Name: "刘备",
Age: 28,
}
fmt.Printf("结构体为:%+v",xiShu)
}
结构体为:{ID:1 Name:刘备 Age:28}
结构体名.成员1 = 值
结构体名.成员2 = 值
package main
import "fmt"
type XiShu struct {
ID int
Name string
Age int
}
func main() {
var xiShu XiShu
xiShu.Name = "刘备"
xiShu.Age = 28
fmt.Printf("结构体为:%+v",xiShu)
}
结果显示
结构体为:{ID:0 Name:刘备 Age:28}
package main
import "fmt"
type XiShu struct {
ID int
Name string
Age int
Position string
}
//checkAge 定义函数,以结构体做参数。
func checkAge(info XiShu)(result string) {
if info.Age >= 18 {
result = info.Name+"是一个成年人"
return result
}else {
info.Name = "小"+info.Name
result = info.Name+"还没有长大"
return result
}
}
func main () {
liuBei := XiShu{
ID: 0,
Name: "刘备",
Age: 28,
Position: "主公",
}
result := checkAge(liuBei)
fmt.Println(result)
}
刘备是一个成年人
- 结构体指针可以直接使用,不需要多余操作。
package main
import "fmt"
type XiShu struct {
ID int
Name string
Age int
Position string
}
//info 是一个结构体的指针
func checkAge(info *XiShu)(result string) {
if info.Age >= 18 {
result = info.Name+"是一个成年人"
return result
}else {
info.Name = "小"+info.Name
result = info.Name+"还没有长大"
return result
}
}
func main () {
liuBei := XiShu{
ID: 0,
Name: "刘备",
Age: 28,
Position: "主公",
}
//将结构体liuBei的指针传入函数
result := checkAge(&liuBei)
fmt.Println(result)
}
package main
import "fmt"
type XiShu struct {
ID int
Name string
Age int
Position string
}
func IncreaseAge(info XiShu)(age int) {
info.Age += 1
fmt.Printf("函数中构体:\t%+v\n",info)
age = info.Age
return age
}
func main () {
liuBei := XiShu{
ID: 0,
Name: "刘备",
Age: 28,
Position: "主公",
}
fmt.Printf("原结构体:\t%+v\n",liuBei)
IncreaseAge(liuBei)
fmt.Printf("当前结构体:\t%+v\n",liuBei)
}
结果
原结构体: {ID:0 Name:刘备 Age:28 Position:主公}
函数中构体: {ID:0 Name:刘备 Age:29 Position:主公}
当前结构体: {ID:0 Name:刘备 Age:28 Position:主公}
如上可见,结构体值变化不会影响函数外。
package main
import "fmt"
type XiShu struct {
ID int
Name string
Age int
Position string
}
func IncreaseAge(info *XiShu)(age int) {
info.Age += 1
fmt.Printf("函数中构体:\t%+v\n",info)
age = info.Age
return age
}
func main () {
liuBei := XiShu{
ID: 0,
Name: "刘备",
Age: 28,
Position: "主公",
}
fmt.Printf("原结构体:\t%+v\n",liuBei)
IncreaseAge(&liuBei)
fmt.Printf("当前结构体:\t%+v\n",liuBei)
}
结果:
原结构体: {ID:0 Name:刘备 Age:28 Position:主公}
函数中构体: &{ID:0 Name:刘备 Age:29 Position:主公}
当前结构体: {ID:0 Name:刘备 Age:29 Position:主公}
如上可见,结构体数据彻底放生变化
函数中的形参和函数外的实参内存地址是不同的。
参数传递时,函数中的形参复制了函数外实参的值,函数结束后形参地址将被释放,这些不会对实参造成影响。
我们将实参的地址作为第二个参数传入函数,然后和第一个形参的地址做比较。
ackage main
import (
"fmt"
)
type XiShu struct {
ID int
Name string
Age int
Position string
}
func IncreaseAge(info XiShu,xiShu *XiShu) {
fmt.Printf("比较形参和实参地址:")
if &info == xiShu {
fmt.Println("相同")
}else {
fmt.Println("不同")
}
}
func main () {
//liuBei := new(XiShu)
liuBei := XiShu{
ID: 0,
Name: "刘备",
Age: 28,
Position: "主公",
}
IncreaseAge(liuBei,&liuBei)
}
结果:
比较形参和实参地址:不同
type StructName02 struct {
……
MemberName StructName01
}
type XiShu struct {
ID int
Name string
Age int
Position string
Equipment Equipment
}
type Equipment struct {
Arms string
Horse string
}
StructName02.MemberName.StructName01Menber
用结构体赋值只能使用
StructName02.MemberName.StructName01Menber
的方法
liuBei := XiShu{
ID: 0,
Name: "刘备",
Age: 28,
Position: "主公",
Equipment: Equipment{
Arms: "双股剑",
Horse: "的卢",
},
}
单个赋值语法中两种方法都可以
liuBei.Equipment.Arms = "双股剑"
package main
import "fmt"
type XiShu struct {
ID int
Name string
Age int
Position string
Equipment Equipment
}
type Equipment struct {
Arms string
Horse string
}
func main () {
//可以用结构体赋值
liuBei := XiShu{
ID: 0,
Name: "刘备",
Age: 28,
Position: "主公",
Equipment: Equipment{
Arms: "双股剑",
//Horse: "的卢", //为了展示直接对成员赋值,此处先注释掉
},
}
//也可以直接对成员赋值
liuBei.Equipment.Horse = "的卢"
fmt.Printf("%+v",liuBei)
}
{ID:0 Name:刘备 Age:28 Position:主公 Equipment:{Arms:双股剑 Horse:的卢}}
type StructName02 struct {
……
StructName01
}
type XiShu struct {
ID int
Name string
Age int
Position string
Equipment
}
type Equipment struct {
Arms string
Horse string
}
StructName02.StructName01Menber
或
StructName02.MemberName.StructName01Menber
用结构体赋值
只能用StructName02.MemberName.StructName01Menber
的结构,而不能使用StructName02.StructName01Menber
的结构
liuBei := XiShu{
ID: 0,
Name: "刘备",
Age: 28,
Position: "主公",
Equipment: Equipment{
Arms: "双股剑",
Horse: "的卢",
},
}
如上,不能
写成:
liuBei := XiShu{
ID: 0,
Name: "刘备",
Age: 28,
Position: "主公",
Arms: "双股剑", //不能这么写
Horse: "的卢", //不能这么写
},
}
StructName02.MemberName.StructName01Menber
的结构和StructName02.StructName01Menber
的结构都可以。
liuBei.Arms = "双股剑"
或
liuBei.Equipment.Arms = "双股剑"
package main
import "fmt"
type XiShu struct {
ID int
Name string
Age int
Position string
Equipment
}
type Equipment struct {
Arms string
Horse string
}
func main () {
//用结构体赋值
liuBei := XiShu{
ID: 0,
Name: "刘备",
Age: 28,
Position: "主公",
//Equipment: Equipment{ //为了展示单个成员赋值效果,此处可以注释掉
// Arms: "双股剑",
// Horse: "的卢",
//},
}
//直接引用`Equipment`的成员
liuBei.Arms = "双股剑"
//通过Equipment引用其成员
liuBei.Equipment.Horse = "的卢"
fmt.Printf("%+v",liuBei)
}
{ID:0 Name:刘备 Age:28 Position:主公 Equipment:{Arms:双股剑 Horse:的卢}}
注意,虽然
Equipment
的成员可以直接引用,但是打印出的结果依然在Equipment
下。
fmt.Println(StructName)
代码:
package main
import "fmt"
type XiShu struct {
ID int
Name string
Age int
Position string
}
func main () {
liuBei := XiShu{
ID: 0,
Name: "刘备",
Age: 28,
Position: "主公",
}
fmt.Println(liuBei)
}
结果输出
{0 刘备 28 主公}
fmt.Printf("%+v",StructName)
代码:
package main
import "fmt"
type XiShu struct {
ID int
Name string
Age int
Position string
}
func main () {
liuBei := XiShu{
ID: 0,
Name: "刘备",
Age: 28,
Position: "主公",
}
fmt.Printf("%+v",liuBei)
}
结果输出
{ID:0 Name:刘备 Age:28 Position:主公}