Golang methods: Difference between revisions

From wikinotes
No edit summary
Line 2: Line 2:
This is most common with structs, but any type is supported
This is most common with structs, but any type is supported


= Adding Methods =
<blockquote>
<syntaxhighlight lang="go">
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)
}
</syntaxhighlight>
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() {
    rect := Rectangle{5, 5}
    fmt.Println(rect.Area())
}
</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>
</blockquote><!-- Local Types -->


= Basics =
= Basics =
Line 8: Line 71:


<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
type Animal struct {
    name string
    age int
}


func (a *Animal) meow() {
func (a *Animal) meow() {
Line 19: Line 78:
}
}


func (a Animal) pounce() {
    // a copy of 'a' is created in memory, and passed to this method
    // (field assignments have no effect outside of this object)
    fmt.Println(a.name + " crouches and pounces")
}


func main() {
func main() {

Revision as of 14:19, 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

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)
}

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)
}

Basics

Methods let you bind functions to existing types.

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


func main() {
    animal := Animal{name: "foo", age: 123}
    animal.meow()
    animal.pounce()
}