鸿蒙 golang,golang struct

struct

1、定义一个structpackage main

import "fmt"

type Rectangle struct {

width float64

height float64

}

func main(){

var r Rectangle       //声明一个结构体  r,width height的值为“零”值。在这里为0.0,0.0

r = Rectangle{width:20,height:10} //给长宽赋值,带名称时,顺序随意

r = Rectangle{20,10}      //等价上部的赋值,不带变量名称时,值与声明的变量顺序一致。

fmt.Println("the Rectangle width:",r.width) // 访问 r.{属性}

}

//执行结果:

the Rectangle width: 20

2、给结构体定义方法package main

import "fmt"

type Rectangle struct {

width float64

height float64

}

func (r *Rectangle) area() float64 { //定义一个area的函数,返回值类型为float64,函数的接收者为前面括号的(变量名 类型名)

return r.width * r.height

}

func main(){

var r Rectangle

r = Rectangle{width:20,height:10}

r = Rectangle{20,10}

fmt.Println("the Rectangle width:",r.width)

fmt.Println("the area of Rectangle: ",r.area())  //直接调用area函数

}

//执行结果:

the Rectangle width: 20

the area of Rectangle:  200   //计算结果为200

3、结构体方法接收类型为指针,则能改变原结构体的属性值

我们先将类型设置为值类型看看package main

import "fmt"

type Rectangle struct {

width float64

height float64

}

func (r *Rectangle) area() float64 {

return r.width * r.height

}

func (r Rectangle) changeWidth(){   //把接收体的类型设置为值类型

r.width = 30

}

func main(){

var r Rectangle

r = Rectangle{width:20,height:10}

r = Rectangle{20,10}

fmt.Println("the Rectangle width:",r.width)

fmt.Println("the area of Rectangle: ",r.area())

r.changeWidth()                 //改变了width

fmt.Println("the Rectangle width:",r.width) //打印结果

}

//执行结果:

the Rectangle width: 20

the area of Rectangle:  200

the Rectangle width: 20               //结果显示并没有改变

我们将接收体设置为指针package main

import "fmt"

type Rectangle struct {

width float64

height float64

}

func (r *Rectangle) area() float64 {

return r.width * r.height

}

func (r *Rectangle) changeWidth(){  // 指针类型

r.width = 30

}

func main(){

var r Rectangle

r = Rectangle{width:20,height:10}

r = Rectangle{20,10}

fmt.Println("the Rectangle width:",r.width)

fmt.Println("the area of Rectangle: ",r.area())

r.changeWidth()

fmt.Println("the Rectangle width:",r.width)

}

//执行结果:

the Rectangle width: 20

the area of Rectangle:  200

the Rectangle width: 30                 //结果显示已经改变了width的值

回答: 在Go语言中,struct是一种数据类型,用于定义自定义的复合数据结构。struct可以包含多个字段,每个字段都有自己的类型和名称。可以使用struct关键字来定义一个struct类型,并在大括号内定义字段。\[1\]在struct中,还可以使用内嵌结构体的方式来组织数据。内嵌结构体是指在一个结构体中嵌入另一个结构体作为其字段。这样可以实现结构体的组合和复用。\[2\]在使用struct时,可以使用简洁的语法格式来创建和初始化结构体变量。可以使用结构体类型名加大括号的方式来创建一个结构体变量,并为每个字段赋值。也可以使用键值对的方式来指定字段的值。\[3\] #### 引用[.reference_title] - *1* *3* [Golang 结构体](https://blog.csdn.net/weixin_47243236/article/details/122028539)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Golang 入门 : 结构体(struct)](https://blog.csdn.net/weixin_30753873/article/details/97302216)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值