Brad Peabody
Brad Peabody

Reputation: 11377

Go interface with String() method

Is there a Go interface in the standard library with:

String() string

?

(Similar to how Java has toString() on java.lang.Object)

Perhaps I just didn't search enough but I didn't see one. Wanted to use one that already exists rather than than making my own (though I guess it really makes no difference with Go's type system).

Upvotes: 5

Views: 992

Answers (2)

Intermernet
Intermernet

Reputation: 19388

fmt.Stringer is what you're after.

type Stringer interface {
        String() string
}

Upvotes: 10

Carl Groner
Carl Groner

Reputation: 4359

The closest thing I've seen to Java's toString is fmt#Stringer.

Upvotes: 4

Related Questions