当前位置:网站首页>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
边栏推荐
- Upgrade MySQL 5.1 to 5.611
- Functions (Part I)
- GFS distributed file system (Theory)
- Control structure (I)
- 如果conda找不到想要安装的库怎么办PackagesNotFoundError: The following packages are not available from current
- Mysql database explanation (IX)
- Crawling fragment of a button style on a website
- PHP PDO ODBC loads files from one folder into the blob column of MySQL database and downloads the blob column to another folder
- 【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷
- Do we media make money now? After reading this article, you will understand
猜你喜欢
随机推荐
Detailed explanation of redirection and request forwarding
pywintypes.com_error: (-2147221020, ‘无效的语法‘, None, None)
一刷312-简单重复set-剑指 Offer 03. 数组中重复的数字(e)
Common types of automated testing framework ▏ automated testing is handed over to software evaluation institutions
Modèle de Cluster MySQL et scénario d'application
MySQL集群模式与应用场景
KNN, kmeans and GMM
Go并发和通道
Mysql database explanation (IX)
cadence SPB17. 4 - Active Class and Subclass
单体架构系统重新架构
Independent operation smart farm Innovation Forum
开源项目推荐:3D点云处理软件ParaView,基于Qt和VTK
MySQL Cluster Mode and application scenario
Functions (Part I)
s16.基于镜像仓库一键安装containerd脚本
PHP PDO ODBC将一个文件夹的文件装载到MySQL数据库BLOB列,并将BLOB列下载到另一个文件夹
【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷
自动化测试框架常见类型▏自动化测试就交给软件测评机构
Timing model: gated cyclic unit network (Gru)