String()%+vString()

示例:https://play.golang.org/p/SxTVOtwVV-9

package main

import (
    "fmt"
)

type Foo struct {
    Jekyl string
    Hyde  string
}

func (foo Foo) String() string {
    return foo.Jekyl // how I want it to show in the rest of the program
}

func main() {
    bar := Foo{Jekyl: "good", Hyde: "evil"}
    fmt.Printf("%+v", bar) // debugging to see what's going on, can't see the evil side
}

输出

good

但我希望看到你没有实现String()方法

{Jekyl:good Hyde:evil}

Tim Abell.. 10

%#v
fmt.Printf("%#v", bar)

输出:

main.Foo{Jekyl:"good", Hyde:"evil"}

参考/sf/ask/17360801/

https://play.golang.org/p/YWIf6zGU-En



1> Tim Abell..:
%#v
fmt.Printf("%#v", bar)

输出:

main.Foo{Jekyl:"good", Hyde:"evil"}

参考/sf/ask/17360801/

https://play.golang.org/p/YWIf6zGU-En