Original post

Learning Interface in GO and made the below code:

package main

import "fmt"

type I interface {
        fullName() string
type Person struct {
        fname, lname string

func (p Person) fullName() string { // can not accept: (p *Person)
        return p.fname + p.lname

func main() {
        var name string = `hasan`
        var p I = Person{fname: name, lname: `yousef`}
        fmt.Printf("Your full name is: %v", p.fullName)


The output I got is:

Your full name is: 0x49c560

Which is not as expected, what is the 0x49c560 and how to get the correct output printed as string?

You defined fullName as a function that retturns a string so change your call to

fmt.Printf("Your full name is: %v", p.fullName())

Yep, exactly as @Yamil_Bracho pointed out, you’ve forgot to actually call the function.

As in Go functions (and methods obviously) are first-class citizens, they can be used as values (assigned, passed as arguments, etc), that’s why it’s completely “legal” to do what you did, but this has different meaning :slight_smile:

One other thing – your comment that says // can not accept: (p *Person) isn’t quite true – you actually can have pointer receivers! Your function could look like:

func (p *Person) fullName() string {
        return p.fname + p.lname

As long you changed the line in your main function to:
var p I = &Person{fname: name, lname: "yousef"}
(Note the & sign before the word Person)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.