当前位置:网站首页>Go语言数组,指针,结构体
Go语言数组,指针,结构体
2022-04-23 15:34:00 【世界尽头与你】
1.数组
声明
Go 语言数组声明需要指定元素类型及元素个数,语法格式如下:
var variable_name [SIZE] variable_type
以上为一维数组的定义方式。例如以下定义了数组 balance 长度为 10 类型为 float32:
var balance [10] float32
初始化数组
balance := [5]float32{
1000.0, 2.0, 3.4, 7.0, 50.0}
如果数组长度不确定,可以使用 ...
代替数组的长度,编译器会根据元素个数自行推断数组的长度:
balance := [...]float32{
1000.0, 2.0, 3.4, 7.0, 50.0}
如果设置了数组的长度,我们还可以通过指定下标来初始化元素:
// 将索引为 1 和 3 的元素初始化
balance := [5]float32{
1:2.0,3:7.0}
访问数组元素
package main
import "fmt"
func main() {
var n [10]int
var i, j int
for i = 0; i < 10; i++ {
n[i] = i + 521
}
for j = 0; j < 10; j++ {
fmt.Printf("%d\n", n[j])
}
}
-------------------------
输出:
521
522
523
524
525
526
527
528
529
530
2.指针
变量在内存中地址
package main
import "fmt"
func main() {
var a int = 521
fmt.Println(&a)
}
----------------------
输出:0xc000018098
什么是指针
一个指针变量指向了一个值的内存地址。
类似于变量和常量,在使用指针前你需要声明指针。指针声明格式如下:
var var_name *var-type
var-type
为指针类型,var_name
为指针变量名,*
号用于指定变量是作为一个指针。以下是有效的指针声明:
var ip *int /* 指向整型*/
var fp *float32 /* 指向浮点型 */
使用指针
package main
import "fmt"
func main() {
var a int = 521
var ip *int
ip = &a
fmt.Println(a, ip, *ip)
}
------------------------------
输出:
521 0xc000018098 521
空指针
当一个指针被定义后没有分配到任何变量时,它的值为 nil
。
nil
指针也称为空指针。
nil
在概念上和其它语言的null
、None
、nil
、NULL
一样,都指代零值或空值。
一个指针变量通常缩写为 ptr
。
空指针判断:
if(ptr != nil) /* ptr 不是空指针 */
if(ptr == nil) /* ptr 是空指针 */
3.结构体
结构体定义需要使用 type
和 struct
语句。struct
语句定义一个新的数据类型,结构体中有一个或多个成员。type
语句设定了结构体的名称。结构体的格式如下:
package main
import "fmt"
type Books struct {
title string
author string
subject string
id int
}
func main() {
var Book1 Books /* 声明 Book1 为 Books 类型 */
Book1.title = "Go语言速通"
Book1.author = "dahe"
Book1.subject = "Go"
Book1.id = 1
fmt.Println(Book1.title, Book1.author, Book1.subject, Book1.id)
}
------------------------
输出:
Go语言速通 dahe Go 1
版权声明
本文为[世界尽头与你]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Gherbirthday0916/article/details/124361011
边栏推荐
- Tun equipment principle
- Multitimer V2 reconstruction version | an infinitely scalable software timer
- Sorting and replying to questions related to transformer
- Precautions for use of dispatching system
- Mobile finance (for personal use)
- Rsync + inotify remote synchronization
- What exactly does the distributed core principle analysis that fascinates Alibaba P8? I was surprised after reading it
- 编译,连接 -- 笔记
- 群体智能自主作业智慧农场项目启动及实施方案论证会议
- Hj31 word inversion
猜你喜欢
2022年中国数字科技专题分析
我的树莓派 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
Detailed explanation of kubernetes (IX) -- actual combat of creating pod with resource allocation list
激活函数的优缺点和选择
G007-hwy-cc-estor-03 Huawei Dorado V6 storage simulator construction
Kubernetes详解(九)——资源配置清单创建Pod实战
推荐搜索 常用评价指标
Analysis of common storage types and FTP active and passive modes
cadence SPB17. 4 - Active Class and Subclass
API gateway / API gateway (II) - use of Kong - load balancing
随机推荐
utils.DeprecatedIn35 因升级可能取消,该如何办
[backtrader source code analysis 18] Yahoo Py code comments and analysis (boring, interested in the code, you can refer to)
Mysql连接查询详解
MySQL query library size
字节面试 transformer相关问题 整理复盘
Today's sleep quality record 76 points
考试考试自用
Deeply learn the skills of parameter adjustment
Rsync + inotify remote synchronization
MySQL sync could not find first log file name in binary log index file error
Detailed explanation of kubernetes (XI) -- label and label selector
Precautions for use of dispatching system
控制结构(一)
Functions (Part I)
Connect PHP to MySQL via PDO ODBC
Modify the default listening IP of firebase emulators
深度学习——超参数设置
让阿里P8都为之着迷的分布式核心原理解析到底讲了啥?看完我惊了
激活函数的优缺点和选择
After time judgment of date