douqin1932 2013-06-07 04:59
浏览 125
已采纳

为什么我的Stringer接口方法没有被调用? 使用fmt.Println时

Suppose I have the following code:

package main

import "fmt"

type Car struct{
    year int
    make string
}

func (c *Car)String() string{
    return fmt.Sprintf("{make:%s, year:%d}", c.make, c.year)
}

func main() {
    myCar := Car{year:1996, make:"Toyota"}
    fmt.Println(myCar)
}

When I call fmt.Println(myCar) and the object in question is a pointer, my String() method gets called properly. If, however the object is a value, my output is formatted using the default formatting built into Go and my code to format the said object is not called.

The interesting thing is in either case if I call myCar.String() manually it works properly whether my object is either a pointer or value.

How can I get my object formatted the way I want no matter if the object is value-based or pointer-based when used with Println?

I don't want to use a value method for String because then that means every time it's invoked the object is copied which seams unreasonable. And I don't want to have to always manually called .String() either because I'm trying to let the duck-typing system do it's work.

  • 写回答

5条回答 默认 最新

  • dqvzfp6468 2013-06-07 07:32
    关注

    When calling fmt.Println, myCar is implicitly converted to a value of type interface{} as you can see from the function signature. The code from the fmt package then does a type switch to figure out how to print this value, looking something like this:

    switch v := v.(type) {
    case string:
        os.Stdout.WriteString(v)
    case fmt.Stringer:
        os.Stdout.WriteString(v.String())
    // ...
    }
    

    However, the fmt.Stringer case fails because Car doesn't implement String (as it is defined on *Car). Calling String manually works because the compiler sees that String needs a *Car and thus automatically converts myCar.String() to (&myCar).String(). For anything regarding interfaces, you have to do it manually. So you either have to implement String on Car or always pass a pointer to fmt.Println:

    fmt.Println(&myCar)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?