当前位置:网站首页>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,表示指针指向的变量.
边栏推荐
猜你喜欢
随机推荐
redis-企业级使用
单点登录原理及实现方式
SQL力扣刷题五
sql力扣刷题六
SQL如何从字符串截取指定字符(LEFT、MID、RIGHT三大函数)
掌握SSR
Web系统常见安全漏洞介绍及解决方案-XSS攻击
AtCoder—E - Σ[k=0..10^100]floor(X/10^k
【JWT】JWT 整合
Open Kylin openKylin automation developer platform officially released
众多mock工具,这一次我选对了
强大的SQL计算利器-SPL
WEB核心【记录网站登录人数,记录用户名案例】Cookie技术实现
Build finished with errors/Executable Not Found
Build finished with errors/Executable Not Found
内网渗透学习(四)域横向移动——SMB和WMI服务利用
SQL去重的三种方法汇总
【LeetCode】1161.最大层内元素和
单点登录的三种方式
浅谈Attention与Self-Attention,一起感受注意力之美