当前位置:网站首页>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
边栏推荐
- Detailed explanation of kubernetes (IX) -- actual combat of creating pod with resource allocation list
- kubernetes之常用Pod控制器的使用
- Collation of errors encountered in the use of redis shake
- Kubernetes详解(九)——资源配置清单创建Pod实战
- Mobile finance (for personal use)
- Comparaison du menu de l'illustrateur Adobe en chinois et en anglais
- 服务器中毒了怎么办?服务器怎么防止病毒入侵?
- Today's sleep quality record 76 points
- What are the mobile app software testing tools? Sharing of third-party software evaluation
- [backtrader source code analysis 18] Yahoo Py code comments and analysis (boring, interested in the code, you can refer to)
猜你喜欢
X509 certificate cer format to PEM format
Detailed explanation of MySQL connection query
Functions (Part I)
Detailed explanation of kubernetes (XI) -- label and label selector
Independent operation smart farm Innovation Forum
群体智能自主作业智慧农场项目启动及实施方案论证会议
Mysql database explanation (IX)
Kubernetes详解(九)——资源配置清单创建Pod实战
Mysql连接查询详解
导入地址表分析(根据库文件名求出:导入函数数量、函数序号、函数名称)
随机推荐
Machine learning - logistic regression
Node. JS ODBC connection PostgreSQL
MySQL installation process (steps for successful installation)
软件性能测试报告起着什么作用?第三方测试报告如何收费?
TLS / SSL protocol details (28) differences between TLS 1.0, TLS 1.1 and TLS 1.2
JVM-第2章-类加载子系统(Class Loader Subsystem)
深度学习——超参数设置
Tun model of flannel principle
字节面试 transformer相关问题 整理复盘
Error: unable to find remote key "17f718f726"“
adobe illustrator 菜单中英文对照
Mysql database explanation (8)
Introduction to dynamic programming of leetcode learning plan day3 (198213740)
Basic concepts of website construction and management
KNN, kmeans and GMM
基础贪心总结
Mysql database explanation (VII)
Deeply learn the skills of parameter adjustment
Summary of interfaces for JDBC and servlet to write CRUD
C language super complete learning route (collection allows you to avoid detours)