# pointer

跟 c 語言的指標相同。

> 至於何時func 的參數要用pointer可參考：
>
> <https://flaviocopes.com/golang-methods-receivers/>

## 傳值與傳址時機

假設我們今天有範例如下，想改變他的年紀

```go
package main

import "fmt"

type User struct {
	Name string
	Age  int
}

func (c User) GetAge() {
	fmt.Println("Age:", c.Age)
}

func UpdateAge(c User, Age int) {
	c.Age = Age
}

func main() {
	c := User{"age", 20}
	c.GetAge()
	UpdateAge(c, 21)
	c.GetAge()
}

```

但結果都是`Age: 20 Age: 20`

所以我們要用傳指標的方式，才會真正改變到原本的物件

```go
package main

import "fmt"

type User struct {
	Name string
	Age  int
}

func (c User) GetAge() {
	fmt.Println("Age:", c.Age)
}

func UpdateAge(c *User, Age int) {
	c.Age = Age
}

func main() {
	c := User{"age", 20}
	c.GetAge()
	UpdateAge(&c, 21)
	c.GetAge()
}
```

> 在 UpdateAge 第一個參數改為傳指標

## 如果有 struct 傳入 func 建議都用指標

因為耗費資源少。

<https://golang.org/doc/faq#different_method_sets>

## Unsafe.pointer

<http://tsunghao.github.io/post/2015/02/manipulating-pointers-in-golang/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://easonwang.gitbook.io/golang/ji-ben-yu-fa/pointer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
