Programming/golang2022. 9. 27. 17:12

메소드라길래 먼가 해서 봤더니.. class는 없는 대신 타입 결정적인 함수를 메소드라고 정의하는 듯.

메소드의 타입을 정의하는 것을 리시버 라고 명명한다.

Methods
Go does not have classes. However, you can define methods on types.

A method is a function with a special receiver argument.

The receiver appears in its own argument list between the func keyword and the method name.

In this example, the Abs method has a receiver of type Vertex named v.
package main

import (
"fmt"
"math"
)

type Vertex struct {
X, Y float64
}

func (v Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main() {
v := Vertex{3, 4}
fmt.Println(v.Abs())
}

[링크 : https://go.dev/tour/methods/1]

[링크 : https://dev-yakuza.posstree.com/ko/golang/method/]

[링크 : https://kamang-it.tistory.com/entry/Go15메소드Method와-리시버Receiver]

 

포인터를 넘길수도 있긴 하다.

[링크 : http://golang.site/go/article/17-Go-메서드]

 

'Programming > golang' 카테고리의 다른 글

golang json/encoding marshal() unmarshal()  (0) 2022.09.28
golang mac address 얻기  (0) 2022.09.28
go mod init 과 go build  (0) 2022.09.27
golang 함수 인자에 함수 넣기  (0) 2022.09.27
golang package main  (0) 2022.09.23
Posted by 구차니