golang fmt.Stringer interfaces use custom formatted output of struct

In Go Stringer interface also has this feature. Stringer fmt interface is defined in the package, the interface comprising String () method. Of any type as long as the definition of the String () method, when Print output can get customized output.

Stringer interfaces are defined as follows:

type Stringer interface {
    String () String
}
Package main

Import (
        "FMT"
)


type the Power struct {
        Age int
        High int
        name String
}

// pointer type
FUNC (the this the Power *) String () {String
        return FMT .Sprintf ( "Age:% D, High:% D, name:% S", this.age, this.high, this.name)
}


FUNC main () {
        var * the Power I = {the Power & Age: 10, High : 178, name: "NewMan" } // pointer type


        fmt.Printf ( "% S \ n-", I)
        fmt.Println (I)
        fmt.Printf ( "
}


Original link: https: //blog.csdn.net/lanyang123456/java/article/details/78178183

Guess you like

Origin www.cnblogs.com/longchang/p/12614218.html