Pointers {.en}

指针 {.zh}

::: {.en} Go supports pointers, allowing you to pass references to values and records within your program. :::

::: {.zh}

Go 支持 指针,允许您在程序中传递对值和数据结构的引用。

:::

  1. package main
  2. import "fmt"

::: {.en} We’ll show how pointers work in contrast to values with 2 functions: zeroval and zeroptr. zeroval has an int parameter, so arguments will be passed to it by value. zeroval will get a copy of ival distinct from the one in the calling function. :::

::: {.zh}

我们将通过对比 2 个函数 zerovalzeroptr 来展示指针是如何工作的。 zeroval 有一个 int 参数,所以参数将通过值传递给它。 zeroval 将从调用它的那个函数中得到一个 ival 形参的拷贝。

:::

  1. func zeroval(ival int) {
  2. ival = 0
  3. }

::: {.en} zeroptr in contrast has an *int parameter, meaning that it takes an int pointer. The *iptr code in the function body then dereferences the pointer from its memory address to the current value at that address. Assigning a value to a dereferenced pointer changes the value at the referenced address. :::

::: {.zh}

相比之下,zeroptr 有一个 *int 参数,这意味着它需要一个 int 指针。函数体中的 *iptr 接着解引用这个指针,从其内存地址得到该地址当前对应的值。将值赋给解引用的指针会改变引用地址的值。

:::

  1. func zeroptr(iptr *int) {
  2. *iptr = 0
  3. }
  4. func main() {
  5. i := 1
  6. fmt.Println("initial:", i)
  7. zeroval(i)
  8. fmt.Println("zeroval:", i)

::: {.en} The &i syntax gives the memory address of i, i.e. a pointer to i. :::

::: {.zh}

&i 语法给出了 i 的内存地址,即指向 i 的指针。

:::

  1. zeroptr(&i)
  2. fmt.Println("zeroptr:", i)

::: {.en} Pointers can be printed too. :::

::: {.zh}

指针也可以被打印。

:::

  1. fmt.Println("pointer:", &i)
  2. }

::: {.en} zeroval doesn’t change the i in main, but zeroptr does because it has a reference to the memory address for that variable. :::

::: {.zh}

zeroval 不会改变 main 中的 i,但是 zeroptr 会改变,因为它有一个对该变量的内存地址的引用。

:::

  1. $ go run pointers.go
  2. initial: 1
  3. zeroval: 1
  4. zeroptr: 0
  5. pointer: 0x42131100