Go Basics: Method Related

Source: Internet
Author: User

Method related

method is a special function with the reciever parameter.

https://tour/golang.org Package Main

Import ("FMT" "math")

Type Vertex struct{

X, Y float64

}

There's no class concept in Go

method, a function with the parameters of the recipient

Func (v Vertex) AbsMethod1 () float64 {

return Math. SQRT (v.x*v.x + v.y*v.y)

}

function ordinary Functions,

Func AbsFunc1 (v Vertex) float64{

return Math. SQRT (v.x*v.x + v.y*v.y)}

Zoom method, with receiver

Func (v *vertex) Scalemethod (f float64) {

v.x = v.x * F v.y = v.y * F}

Zoom, function, and pointers, where the pointer is more like a reference

Func Scalefunc (v *vertex, f float64) {

v.x *= F v.y *= F}

Func Main () {

V: = vertex{3, 4}

Fmt. Println ("=====method abs=", V.absmethod ())

Fmt. Println ("=====function Abs =", AbsFunc1 (v))

V.scalemethod (Ten) fmt. Println ("vvvv===", V)

Fmt. Println (V.absmethod ()) Scalefunc (&v, 2)

Fmt. Println (v)

P: = &v p.absmethod1 ()//Here P.absmethod1 () is interpreted as (*p). ABSMETHOD1 ()

}

Go Basics: Method Related

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.