当前位置:网站首页>Can I find a Go job in 7 days?Learn Go with arrays and pointers
Can I find a Go job in 7 days?Learn Go with arrays and pointers
2022-07-31 10:37:00 【dream eraser】
学习之前
对于一个 Python 工程师来说,数组和指针在 Python There is no clear concept,So this part of the knowledge can only be relied on C/C++的积累了.
学习数组的时候,可以参考 Python list for practice.
Go 数组
Let's look at the definition of an array first,It is a sequence of fixed-length elements of a particular data type,其中有两个关键点,One is that the array length is fixed,The second is a specific data type.
其语法结构如下:
var variable_name [SIZE] varuable_type
// var 变量名 [元素长度/数量] 数据类型
The number of elements must be an integer type,The data type can be any primitive type,Of course array types are included,In this case, a multidimensional array is formed.
package main
import "fmt"
func main() {
var a_array [3]int
fmt.Println(a_array [0]) // 打印第一个元素
fmt.Println(a_array [1]) // 打印第二个元素
}
测试代码之后,It will be found that the array is not initialized before,The default value is the zero value of the data type,There is also the problem of index value for arrays,This has the basis of other languages,很容易理解.
Let's take a look at how to initialize an array.
package main
import "fmt"
func main() {
var a_array1 [3]int = [3]int{
1, 3, 5}
var a_array2 [3]int = [3]int{
4, 6}
fmt.Println(a_array1[2])
fmt.Println(a_array2[2])
}
You can see that the array output results are displayed as follows:
5
0
The second array variable a_array2 Because there is no initialization,So show it directly as 0 .
The above is when initializing the array,All know the length of the array,如果不确定数组长度,可以使用 ... 对数组进行初始化.
package main
import "fmt"
func main() {
a_array := [...]int{
1, 2, 3}
fmt.Printf("%T\n", a_array)
}
打印输出的 %T 表示输出类型.
After the array has a declared length,无法修改,You can reproduce this knowledge point with code.
Go 指针
在 Go 中,取地址符是 &,使用 &var 就可以获取 var 的内存地址.
测试代码如下所示:
package main
import "fmt"
func main() {
var vari1 int = 10
fmt.Printf("变量地址:%x\n", &vari1)
}
输出结果如下所示:
变量地址:c000012088
Go 指针和 C Pointer-like concept,A pointer variable is a declaration of a variable that points to a memory address.
在使用前,Advance declaration is required,格式如下:
var var_name *var_type
其中 var_type 为指针类型,var_name 是指针变量名,特殊符号 * 标记指针.
For example we declare an integer pointer,The following formats can be used.
var int_ptr *int
A pointer if declared but not assigned a value,默认值是 nil,We are used to the abbreviation of pointer is ptr.
在 Go Each variable in has an address,指针的值就是地址.
Next, we can carry out the following experiment,get from pointer【指针指向的值】,代码如下:
var vari1 int = 10
fmt.Printf("变量地址:%x\n", &vari1)
// 声明一个变量 ptr,Used to store variable addresses
ptr := &vari1
// 输出 ptr 类型
fmt.Printf("ptr 类型:%T\n", ptr)
// 打印 ptr 的值
fmt.Println("ptr 值:", ptr)
// 对指针取值
value := *ptr
fmt.Printf("值类型:%T\n", value)
// 获取指针指向的值
fmt.Println("指针指向的值:", value)
输出内容如下所示:
变量地址:c000012088
ptr 类型:*int
ptr 值: 0xc000012088
值类型:int
指针指向的值: 10
Here we will compare the address operator(&)和取值操作符(*)的区别.
&:取出地址;*:根据地址取值.
The next step is to implement a pointer to modify the value.
func main() {
var vari1 int = 10
fmt.Printf("变量地址:%x\n", &vari1)
// 声明一个变量 ptr,Used to store variable addresses
ptr := &vari1
// 对指针取值
value := *ptr
// 获取指针指向的值
fmt.Println("修改前,指针指向的值:", value)
// 修改指针 ptr 指向的值
*ptr = 444
// 获取指针指向的值
fmt.Println("修改后,vari1 值:", vari1)
}
此时运行代码,发现通过 *ptr The variable has been successfully modified vari1 的值.
The only point of knowledge to ponder here is * where the operator appears,if it appears to the right of the equals sign,表示取指针的值,to the left of the equals sign,表示指针指向的变量.
边栏推荐
- SQL study notes - REGEXP operator
- Chapter Six
- 【LeetCode】203.移除链表元素
- 突破传统可靠性测试:混沌工程优秀实践
- 【LeetCode】242. 有效的字母异位词
- 【GORM】存取数组/自定义类型数据
- Summary of three methods for SQL deduplication
- The big-eyed Google Chrome has also betrayed, teach you a trick to quickly clear its own ads
- Echart饼图添加轮播效果
- Day113. Shangyitong: user authentication, Alibaba Cloud OSS, patient management
猜你喜欢

Centos7 install mysql5.7

KVM virtualization job

Web系统常见安全漏洞介绍及解决方案-sql注入

Make your own dataset in FCN and train it

医院管理系统数据库,课程设计,SQLserver,纯代码设计

Build finished with errors/Executable Not Found

众多mock工具,这一次我选对了

SQL去重的三种方法汇总

Gradle series - Groovy overview, basic use (based on Groovy document 4.0.4) day2-1

Chapter VII
随机推荐
IBM SPSS Statistics 28软件安装包下载及安装教程
Add a shuffling effect to every pie
The fifth chapter
强大的SQL计算利器-SPL
NowCoderTOP28-34 binary tree - continuous update ing
Centos7 install mysql5.7
Rich text editor Tinymce
可以用聚酯树脂将接线板密封接线盒吗?(接线盒灌封胶用哪种树脂)
金鱼哥RHCA回忆录:CL210管理OPENSTACK网络--开放虚拟网络(OVN)简介(课后练习)
csdn文件导出为pdf
Open Kylin openKylin automation developer platform officially released
Windows安装mysql详细步骤(通俗易懂,简单上手)
MySQL中JOIN的用法
Day113. Shangyitong: user authentication, Alibaba Cloud OSS, patient management
csdn file export to pdf
项目管理工具之燃尽图:动态考核团队工作能力
内网渗透学习(四)域横向移动——SMB和WMI服务利用
Windows系统Mysql8版本的安装教程
nodeJs--url模块
NowCoderTOP17-22 二分查找/排序——持续更新ing