当前位置:网站首页>Go language, array, pointer, structure
Go language, array, pointer, structure
2022-04-23 15:41:00 【The end of the world and you】
Go Language array , The pointer , Structure
1. Array
Statement
Go Language array declaration needs to specify the element type and number of elements , The syntax is as follows :
var variable_name [SIZE] variable_type
The above is the definition of one-dimensional array . For example, the following defines an array balance The length is 10 The type is float32:
var balance [10] float32
Initialize array
balance := [5]float32{
1000.0, 2.0, 3.4, 7.0, 50.0}
If the length of the array is uncertain , have access to ... Instead of the length of the array , The compiler will infer the length of the array according to the number of elements :
balance := [...]float32{
1000.0, 2.0, 3.4, 7.0, 50.0}
If you set the length of the array , We can also initialize elements by specifying subscripts :
// Index is 1 and 3 Element initialization of
balance := [5]float32{
1:2.0,3:7.0}
Accessing array elements
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])
}
}
-------------------------
Output :
521
522
523
524
525
526
527
528
529
530
2. The pointer
Address of variable in memory
package main
import "fmt"
func main() {
var a int = 521
fmt.Println(&a)
}
----------------------
Output :0xc000018098
What is a pointer
A pointer variable points to the memory address of a value .
Similar to variables and constants , You need to declare pointers before using them . The format of the pointer declaration is as follows :
var var_name *var-type
var-type Is pointer type ,var_name Is the pointer variable name ,* The sign is used to specify that the variable is used as a pointer . Here is a valid pointer declaration :
var ip *int /* Directed integer */
var fp *float32 /* Point to floating point */
Use the pointer
package main
import "fmt"
func main() {
var a int = 521
var ip *int
ip = &a
fmt.Println(a, ip, *ip)
}
------------------------------
Output :
521 0xc000018098 521
Null pointer
When a pointer is defined and not assigned to any variable , Its value is nil.
nil A pointer is also called a null pointer .
nil Conceptually and in other languages null、None、nil、NULL equally , Both refer to zero value or null value .
A pointer variable is usually abbreviated to ptr.
Null pointer judgment :
if(ptr != nil) /* ptr It's not a null pointer */
if(ptr == nil) /* ptr Is a null pointer */
3. Structure
Structure definition needs to use type and struct sentence .struct Statement to define a new data type , A structure has one or more members .type Statement sets the name of the structure . The format of the structure is as follows :
package main
import "fmt"
type Books struct {
title string
author string
subject string
id int
}
func main() {
var Book1 Books /* Statement Book1 by Books type */
Book1.title = "Go Language express "
Book1.author = "dahe"
Book1.subject = "Go"
Book1.id = 1
fmt.Println(Book1.title, Book1.author, Book1.subject, Book1.id)
}
------------------------
Output :
Go Language express dahe Go 1
Copyright notice : This tutorial is based on the rookie tutorial
版权声明
本文为[The end of the world and you]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231533524018.html
边栏推荐
- Mysql database explanation (IX)
- Machine learning - logistic regression
- Go语言数组,指针,结构体
- Codejock Suite Pro v20.3.0
- 【backtrader源码解析18】yahoo.py 代码注释及解析(枯燥,对代码感兴趣,可以参考)
- cadence SPB17. 4 - Active Class and Subclass
- Leetcode学习计划之动态规划入门day3(198,213,740)
- 山寨版归并【上】
- 自动化测试框架常见类型▏自动化测试就交给软件测评机构
- 一刷314-剑指 Offer 09. 用两个栈实现队列(e)
猜你喜欢

Mumu, go all the way

【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷

Application of Bloom filter in 100 million flow e-commerce system

多级缓存使用

现在做自媒体能赚钱吗?看完这篇文章你就明白了

Temporal model: long-term and short-term memory network (LSTM)

激活函数的优缺点和选择

布隆过滤器在亿级流量电商系统的应用

Recommended search common evaluation indicators

ICE -- 源码分析
随机推荐
c语言---字符串+内存函数
Connect PHP to MSSQL via PDO ODBC
MultiTimer v2 重构版本 | 一款可无限扩展的软件定时器
激活函数的优缺点和选择
cadence SPB17.4 - Active Class and Subclass
Machine learning - logistic regression
The El tree implementation only displays a certain level of check boxes and selects radio
Load Balancer
Upgrade MySQL 5.1 to 5.69
Codejock Suite Pro v20.3.0
自动化测试框架常见类型▏自动化测试就交给软件测评机构
服务器中毒了怎么办?服务器怎么防止病毒入侵?
Go语言数组,指针,结构体
字符串最后一个单词的长度
Upgrade MySQL 5.1 to 5.66
MySQL optimistic lock to solve concurrency conflict
s16. One click installation of containerd script based on image warehouse
Mysql database explanation (8)
小程序知识点积累
Modèle de Cluster MySQL et scénario d'application