go语言结构体struct初始化六种方法
本程序主要测试初始化结构体的几种方法
本程序主要测试初始化结构体的几种方法
package main
import (
"fmt"
)
//定义结构体
type shape struct {
width int
height int
}
//初始化结构体四种方法
func main(){
//方法1
shape1 := new(shape)
fmt.Printf(" %d\n",shape1.width) //输出0
//方法2
shape2 := &shape{}
fmt.Printf(" %d\n",shape2.width)
//方法3
shape3 := &shape{10, 20} //这种需要赋值参数与结构体的变量个数一致, shape3 := &shape{10} 这样赋值是错误的
fmt.Printf(" %d\n",shape3.width)
//方法4
shape4 := &shape{width:10, height:20}
fmt.Printf(" %d\n",shape4.height)
//方法5
var shape5 shape
fmt.Printf(" %d\n",shape5.height)
//方法6
var shape6=shape{width:1} //只对部分成员变量进行初始化
fmt.Printf(" %d\n",shape6.width)
}
输出结果:
> Environment:
> GOPATH=C:\Users\qingshuic\go
> Directory: C:\Program Files\Go\my-jin
> Command: "C:\Program Files\Go\bin\go.exe" run -v "C:\Program Files\Go\my-jin\初始化结构体.go"
> Output:
command-line-arguments
0
0
10
20
0
1
> Elapsed: 1.594s
> Result: Success
设备的序列号SN,生产厂商等,是不是也是写入BIOS芯片中的,在现代操作系统中无法修改
source insight 3.5版本也可以这样支持golang?
感谢大佬的分享,希望加个微信好友,我的微信:TobeBuda
我是C语言程序员
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2022-02-19 route -n 讲解
2022-02-19 解决一次calico异常情况,pod之间访问pod ip不通