본문 바로가기
Go

THEORY - Pointer

by HMangoo 2022. 2. 9.
/*
    pointer
     - & : address
     - * : see through(look through)
*/
package main

import "fmt"

func main() {
    q := 2
    p := q
    q = 10
    fmt.Println(p, " : 메모리에 접근하지 않았을 때")

    a := 2
    b := &a // b : a의 메모리주소, "b"는 a를 살펴보는 "pointer"
    a = 10
    fmt.Println(*b, " : 메모리에 접근하였을 때") // 메모리 주소를 살펴봄

    *b = 20
    fmt.Println(a, " : b를 이용하여 a의 값 변경하기")

    // => heavy project에서 복사본을 만들지 않고 메모리에 저장된 object를 가져오는 것이 프로그램의 속도에 영향
}
 
출력

 

'Go' 카테고리의 다른 글

THEORY - Map  (0) 2022.02.09
THEORY - Array&Slice  (0) 2022.02.09
THEORY - If & switch  (0) 2022.02.09
THEORY - for (feat. range, ...args)  (0) 2022.02.09
THEORY - Function2  (0) 2022.02.09

댓글