5

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).

2
  • 1
    You could use the implements tool to answer questions of this form. In this case, you'd find any package that has structs with String() methods, say, time, which has quite a few, and then run implements -types time, and look at the interfaces it says are implemented by the various time types. Commented Oct 10, 2013 at 7:36
  • Nice - just tried it. Works well. Commented Oct 10, 2013 at 20:53

2 Answers 2

10

fmt.Stringer is what you're after.

type Stringer interface {
        String() string
}
Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.