receiver function returns memory address instead on int

Issue

I wrote a receiver function that should return an integer which is the number of an address of a house (a struct that defined). Please see my code below.

When I call the getnumber receiver function, I get what looks like a memory address (0x47dfc0). I should get 200. I cannot figure out why.

If simply do: fmt.Println(foo_house) I get what I expect: {200 Barrington}

package main

import "fmt"

type house struct {
    number int
    street string
}

// receiver function
func (h house) get_number() int {
    return h.number
}

func main() {
    var foo_house house
    foo_house.number = 200
    foo_house.street = "Barrington"
    n := foo_house.get_number
    fmt.Println(foo_house)
    fmt.Println(n)
}

Solution

What you are getting is the address of the method get_number:

You need to evaluate get_number to get its value, like:

n := foo_house.get_number()

Answered By – Axel Bayerl

Answer Checked By – Jay B. (GoLangFix Admin)

Leave a Reply

Your email address will not be published.