Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

go 구조체와 포인터변수

package main

import "fmt"

type Rectangle struct {
    width, height int
}

func rectangleScalaA(rect *Rectangle, factor int){
    rect.width = rect.width * factor
    rect.height = rect.height * factor
}


func rectangleScalaB(rect Rectangle, factor int){
    rect.width = rect.width * factor
    rect.height = rect.height * factor
}

func main() {
    rect1 := Rectangle{30, 30}
    rectangleScalaA(&rect1, 10)
    fmt.Println(rect1) //주소를 넘겨주기 때문에 바뀐 값이 들어감
    
    rect2 := Rectangle{30, 30}
    rectangleScalaB(rect2, 10)
    fmt.Println(rect2) //값을 넘겨주기 때문에 rect2는 변하지 않음.
}

/*
    포인터 변수는
    pointerVar = &{} 형태
    그래서 &pointerVar은 주소를 나타내고
    *pointerVar는 주소의 값을 나타낸다.
*/

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.