当前位置:网站首页>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
边栏推荐
猜你喜欢

Neodynamic Barcode Professional for WPF V11. 0

大型互联网为什么禁止ip直连

CVPR 2022 优质论文分享

Mysql database explanation (IX)

Configuration of multi spanning tree MSTP

Basic concepts of website construction and management

MySQL Cluster Mode and application scenario

Mobile finance (for personal use)

T2 icloud calendar cannot be synchronized

JVM-第2章-类加载子系统(Class Loader Subsystem)
随机推荐
如果conda找不到想要安装的库怎么办PackagesNotFoundError: The following packages are not available from current
激活函数的优缺点和选择
pgpool-II 4.3 中文手册 - 入门教程
What role does the software performance test report play? How much is the third-party test report charged?
Introduction to dynamic programming of leetcode learning plan day3 (198213740)
今日睡眠质量记录76分
Summary of interfaces for JDBC and servlet to write CRUD
Do we media make money now? After reading this article, you will understand
Deeply learn the skills of parameter adjustment
What if the package cannot be found
WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令
PHP classes and objects
Connectez PHP à MySQL via aodbc
码住收藏▏软件测试报告模板范文来了
utils.DeprecatedIn35 因升级可能取消,该如何办
小程序知识点积累
Explanation 2 of redis database (redis high availability, persistence and performance management)
多级缓存使用
电脑怎么重装系统后显示器没有信号了
Upgrade MySQL 5.1 to 5.68