7 天能找到 Go 工作吗?学学 Go 数组和指针试试

学习之前

对于一个 Python 工程师来说,数组和指针在 Python 中都没有明确概念,所以这部分知识点只能依赖 C/C++的积累了。

学习数组的时候,可以参考 Python 的列表进行实践。

Go 数组

让我们来看看数组的定义,它是特定数据类型的定长元素序列,有两个关键点,一个是数组的固定长度,另一个是特定的数据类型。

[En]

Let’s take a look at the definition of an array, which is a sequence of fixed-length elements of a specific data type, with two key points, one of which is the fixed length of the array, and the other is the specific data type.

其语法结构如下:

var variable_name [SIZE] varuable_type
// var 变量名 [元素长度/数量] 数据类型

元素的数量必须是整数类型,数据类型可以是任何基本类型,包括数组类型,在这种情况下将形成多维数组。

[En]

The number of elements must be an integer type, and the data type can be any basic type, including an array type, in which case a multidimensional array will be formed.

package main

import "fmt"

func main() {

    var a_array [3]int
    fmt.Println(a_array [0]) // 打印第一个元素
    fmt.Println(a_array [1]) // 打印第二个元素
}

测试代码后会发现,在数组初始化之前,缺省值是数据类型的零值,数组还有索引值的问题,这是基于其他语言的,很容易理解。

[En]

After testing the code, you will find that before the array is initialized, the default value is the zero value of the data type, and there is also a problem of index value for the array, which is based on other languages and is easy to understand.

让我们来看看如何初始化数组。

[En]

Let’s take a look at how to initialize arrays.

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])
}

您可以看到,数组输出显示如下:

[En]

You can see that the array output is displayed as follows:

5
0

第二个数组变量 a_array2 由于没有做初始化,所以直接展示为 0 。

上述在初始化数组的时候,都是知道数组长度的,如果不确定数组长度,可以使用 ... 对数组进行初始化。

package main

import "fmt"

func main() {

    a_array := [...]int{1, 2, 3}
    fmt.Printf("%T\n", a_array)
}

打印输出的 %T 表示输出类型。

数组一旦声明长度之后,无法修改,该知识点你可以用代码复现一下。

Go 指针

在 Go 中,取地址符是 &,使用 &var 就可以获取 var 的内存地址。

测试代码如下所示:

package main

import "fmt"

func main() {

    var vari1 int = 10

    fmt.Printf("变量地址:%x\n", &vari1)
}

输出结果如下所示:

变量地址:c000012088

Go 指针和 C 指针一样的概念,指针变量就是声明一个指向内存地址的变量。

在使用之前,您需要提前申报,格式如下:

[En]

Prior to use, you need to declare in advance, in the following format:

var var_name *var_type

其中 var_type 为指针类型, var_name 是指针变量名,特殊符号 * 标记指针。

例如,如果我们声明一个整数指针,我们可以使用以下格式。

[En]

For example, if we declare an integer pointer, we can use the following format.

var int_ptr *int

指针如果被声明但未被赋值,默认值是 nil,大家习惯指针的缩写是 ptr

在 Go 中每个变量都有地址,指针的值就是地址。

接下来,我们可以进行以下实验,从指针中获取[指针指向的值]。代码如下:

[En]

Next, we can do the following experiment to get [the value pointed to by the pointer] from the pointer. The code is as follows:

var vari1 int = 10

fmt.Printf("变量地址:%x\n", &vari1)

// 声明一个变量 ptr,用于存储变量地址
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

这里就要对比一下取地址操作符( &)和取值操作符( *)的区别。

  • &:取出地址;
  • *:根据地址取值。

下一步是实现一个由指针修改的值。

[En]

The next step is to implement a value that is modified by a pointer.

func main() {

    var vari1 int = 10

    fmt.Printf("变量地址:%x\n", &vari1)

    // 声明一个变量 ptr,用于存储变量地址
    ptr := &vari1

    // 对指针取值
    value := *ptr

    // 获取指针指向的值
    fmt.Println("修改前,指针指向的值:", value)

    // 修改指针 ptr 指向的值
    *ptr = 444

    // 获取指针指向的值
    fmt.Println("修改后,vari1 值:", vari1)
}

此时运行代码,发现通过 *ptr 已经成功修改了变量 vari1 的值。

这里唯一要琢磨的知识点是 * 操作符出现的位置,如果其出现在等号右侧,表示 取指针的值,放到等号左侧,表示 指针指向的变量

Original: https://blog.51cto.com/cnca/5570023
Author: 梦想橡皮擦
Title: 7 天能找到 Go 工作吗?学学 Go 数组和指针试试

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/500843/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球