Golang methods: Difference between revisions

From wikinotes
(Created page with "While Go is not an OOP language, it lets you bind methods to any type.<br> This is most common with structs, but any type is supported = Basics = <blockquote> <syntaxhighlig...")
 
 
(4 intermediate revisions by the same user not shown)
Line 3: Line 3:




= Basics =
= Adding Methods =
<blockquote>
<blockquote>
Basics
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
type Animal struct {
type Rectangle struct {
     name string
     width int
     age int
     height int
}
}


func (a *Animal) meow() {
func (a Rectangle) Area() int {
     // a reference to 'a' is passed to this method
     return a.width * a.height
    // (field assignments change original instance)
    fmt.Println((*a).name + " says: meooow")
}
}


func (a Animal) pounce() {
func main() {
     // a copy of 'a' is created in memory, and passed to this method
    rect := Rectangle{width: 10, height: 10}
     // (field assignments have no effect outside of this object)
    fmt.Println(rect.Area() == 100)
     fmt.Println(a.name + " crouches and pounces")
}
</syntaxhighlight>
 
Pointers as self
 
Keep in mind that go parameters are value types and will contain copies of your object.<br>
If working with large objects, it may be more efficient to refer to the object as a pointer.
 
<syntaxhighlight lang="go">
type Rectangle struct {
    width int
    height int
}
 
func (a *Rectangle) Area() int {
     // manually de-reference pointer 'a' to get it's value
    return (*a).width * (*a).height
}
 
func (a *Rectangle) Perimeter() int {
     // go knows you are working with a pointer, you can skip the '(*a)'
     return (a.width * 2) + (a.height * 2)
}
}


func main() {
func main() {
     animal := Animal{name: "foo", age: 123}
     rect := Rectangle{5, 5}
     animal.meow()
     fmt.Println(rect.Area())
    animal.pounce()
}
}
</syntaxhighlight>
</blockquote><!-- Adding Methods -->
= Local Types =
<blockquote>
You can also create local wrapper types for other types, <br>
which lets you add methods, only in a specific localized scope
<syntaxhighlight lang="go">
type Int int


func (i Int) Double() Int {
    return i * 2
}
func main() {
    five := Int(5)
    fmt.Println(five.Double() == 10)
}
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Basics -->
</blockquote><!-- Local Types -->
 
= Method Overloading =
<blockquote>
Not supported in go.
</blockquote><!-- Function Overloading -->

Latest revision as of 15:42, 6 June 2022

While Go is not an OOP language, it lets you bind methods to any type.
This is most common with structs, but any type is supported


Adding Methods

Basics

type Rectangle struct {
    width int
    height int
}

func (a Rectangle) Area() int {
    return a.width * a.height
}

func main() {
    rect := Rectangle{width: 10, height: 10}
    fmt.Println(rect.Area() == 100)
}

Pointers as self

Keep in mind that go parameters are value types and will contain copies of your object.
If working with large objects, it may be more efficient to refer to the object as a pointer.

type Rectangle struct {
    width int
    height int
}

func (a *Rectangle) Area() int {
    // manually de-reference pointer 'a' to get it's value
    return (*a).width * (*a).height
}

func (a *Rectangle) Perimeter() int {
    // go knows you are working with a pointer, you can skip the '(*a)'
    return (a.width * 2) + (a.height * 2)
}

func main() {
    rect := Rectangle{5, 5}
    fmt.Println(rect.Area())
}

Local Types

You can also create local wrapper types for other types,
which lets you add methods, only in a specific localized scope

type Int int

func (i Int) Double() Int {
    return i * 2
}

func main() {
    five := Int(5)
    fmt.Println(five.Double() == 10)
}

Method Overloading

Not supported in go.