galaxyan
galaxyan

Reputation: 6121

Go fmt.Println display wrong contain

I am studying GO by using "a tour of go"
The code is doing very simple things, combine first and last together and output on screen.
The output is a hex address instead of "aaabbb" after I run the code. Could anyone can help me out? Thank you

package main

import "fmt"

type Name struct{
    first,last string
}

func (name Name) fullName() string{
    return (name.first + name.last)
}

func main(){
    v := Name{"aaa","bbb"}
    fmt.Println(v.fullName)
}

Upvotes: 2

Views: 107

Answers (2)

fabmilo
fabmilo

Reputation: 48330

You are not calling the function fullName. you are just passing 'the pointer' to it: see this http://play.golang.org/p/GjibbfoyH0

package main

import "fmt"

type Name struct {
    first, last string
}

func (name Name) fullName() string {
    return (name.first + name.last)
}

func main() {
    v := Name{"aaa", "bbb"}
    fmt.Println(v.fullName())
}

Upvotes: 5

peterSO
peterSO

Reputation: 166598

Use the result of the method

    fmt.Println(v.fullName())

not the address of the method

    fmt.Println(v.fullName)

For example,

package main

import "fmt"

type Name struct{
    first,last string
}

func (name Name) fullName() string{
    return (name.first + name.last)
}

func main(){
    v := Name{"aaa","bbb"}
    fmt.Println(v.fullName())
}

Output:

aaabbb

Upvotes: 5

Related Questions