본문 바로가기
go

구조체 메서드

by Younji! 2016. 12. 19.

func (r *rect) area() int {
    return r.width * r.height
}

func (r rect) perim() int {
    return 2*r.width + 2*r.height
}

type rect struct {

width, height int

}


rect 구조체에 메서드를 위와 같이 개발할 수 있다.


리시버 변수를 일반 구조체와 포인터를 받을 수 있다.


또한, 리시버 변수를 사용하지 않으면 생략하여 사용할 수 있다.

func (_ rect) print() {
    fmt.Println("rect print")
}


https://gobyexample.com/methods

'go' 카테고리의 다른 글

GO tour 41  (0) 2016.04.26

댓글