Go 포인터 리시버

Jmnote (토론 | 기여)님의 2023년 3월 29일 (수) 10:50 판 (새 문서: ==개요== ;Go pointer receivers ;Go 포인터 리시버 <syntaxhighlight lang='go' run> package main import ( "fmt" "math" ) type Vertex struct { X, Y float64 } func (v Vertex)...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요

Go pointer receivers
Go 포인터 리시버
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 (v *Vertex) Scale(f float64) {
	v.X = v.X * f
	v.Y = v.Y * f
}

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

2 같이 보기

3 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}