# pointer

포인터는 메모리 주소를 값으로 가진다.

## pointer를 왜 쓰나

value를 다른 변수에 넣게 되면 모든 value가 복사가 된다. struct도 복사된다. 그럼으로 value가 크면 클수록 복사하는데 시간이 오래걸린다. 퍼포먼스가 문제가 있음. 그래서 pointer를 쓴다.

value를 복사하지 않고 value를 가리키는 주소를 복사한다.

## pointer를 쓰는 방법

```go
var a int
var p *int
p = &a
*p = 100 #p가 가르키는 주소값이 100으로 바뀜

type Data struct {
  value int
}

func ChangeData (arg \*Data) {
  // (*args).value 원래는 이렇게 써야하나 아래처럼 써도 된다.
  arg.value
}

changeData(&100)

var data Data
var p *Data = &data
var p *Data = &Data{xxx}
```

Instance : 메모리에 할당된 실체, 포인터를 사용하면 1개의 instance가 만들어진다.

```sh
var a *Data = &Data{xxx}
```

Instance는 Data의 실체이다.

구조체 포인터를 함께 매개변수로 받는다는 말은 구조체 Instance로 입력을 받겟다는 이야기와 같다.

## new를 이용한 내장 함수

1. P1 := new(Data)
2. P1 := \&Data{} 같은 말이다.

1번은 초기화 불가능 2번은 초기화 가능

Escape Analyzer (탈출분석)을 해서 memory에서 지우지 않게 한다.


---

# 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://teamsmiley.gitbook.io/devops/go-lang/study/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.
