当前位置:网站首页>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
边栏推荐
- G007-hwy-cc-estor-03 Huawei Dorado V6 storage simulator construction
- Common types of automated testing framework ▏ automated testing is handed over to software evaluation institutions
- Error: unable to find remote key "17f718f726"“
- cadence SPB17.4 - Active Class and Subclass
- 推荐搜索 常用评价指标
- Sorting and replying to questions related to transformer
- Comparaison du menu de l'illustrateur Adobe en chinois et en anglais
- T2 icloud calendar cannot be synchronized
- 编译,连接 -- 笔记
- Mysql database explanation (10)
猜你喜欢

Mysql database explanation (VII)

Openstack theoretical knowledge

深度学习——超参数设置

Special analysis of China's digital technology in 2022

setcontext getcontext makecontext swapcontext

Explanation 2 of redis database (redis high availability, persistence and performance management)

Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project

Mysql database explanation (10)

What exactly does the distributed core principle analysis that fascinates Alibaba P8? I was surprised after reading it

cadence SPB17. 4 - Active Class and Subclass
随机推荐
移动app软件测试工具有哪些?第三方软件测评小编分享
山寨版归并【上】
Mobile finance (for personal use)
移动金融(自用)
PHP PDO ODBC loads files from one folder into the blob column of MySQL database and downloads the blob column to another folder
C language super complete learning route (collection allows you to avoid detours)
通过 PDO ODBC 将 PHP 连接到 MySQL
Summary of interfaces for JDBC and servlet to write CRUD
HJ31 单词倒排
Explanation of redis database (IV) master-slave replication, sentinel and cluster
什么是CNAS认证?CNAS认可的软件测评中心有哪些?
About UDP receiving ICMP port unreachable
Hj31 word inversion
Wechat applet customer service access to send and receive messages
Tun equipment principle
Baidu written test 2022.4.12 + programming topic: simple integer problem
字节面试 transformer相关问题 整理复盘
php类与对象
携号转网最大赢家是中国电信,为何人们嫌弃中国移动和中国联通?
What if the server is poisoned? How does the server prevent virus intrusion?