포인터 변수 선언

var numPtr *int // 포인터형 변수 선언시 nil로 초기화

빈 포인터형 변수는 바로 사용 x

new 함수로 메모리 할당 필요

// 포인터 변수 = new(자료형)
var numPtr *int = new(int)

fmt.Println(numPtr) // 메모리 주소 출력

new 함수란

지정한 자료형 크기에 맞는 메모리 공간 할당

// staging/src/k8s.io/apimachinery/pkg/runtime/converter_test.go
// 구조체 내 포인터 변수
type G struct {
	CustomValue1   CustomValue    `json:"customValue1"`
	CustomValue2   *CustomValue   `json:"customValue2"`
	CustomPointer1 CustomPointer  `json:"customPointer1"`
	CustomPointer2 *CustomPointer `json:"customPointer2"`
}

역참조(dereference)

포인터 변수에 값을 대입하거나 가져올려면 역참조를 사용

var numPtr *int = new(int) // new 함수로 공간 할당
*numPtr = 1 // 역참조로 포인터형 변수에 값을 대입
fmt.Println(*numPtr) // 출력: 1, 포인터형 변수에서 값 가져오기