Go String(): Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
Line 71: Line 71:
</syntaxhighlight>
</syntaxhighlight>
IDEs may detect this anti-pattern via static checking and warn that "Placeholder argument causes a recursive call to the 'String' method (%v)".
IDEs may detect this anti-pattern via static checking and warn that "Placeholder argument causes a recursive call to the 'String' method (%v)".
A solution is to convert the argument to the basic string type with <code>string(s)</code>, which does not have this method.

Latest revision as of 01:35, 31 August 2024

External

Internal

Overview

Java developers are used to declare a public String toString() {...} method in their class to get instances of that class rendered as custom Strings. Go has a similar facility, the String() string method.

The default format of values of a custom type can be controlled by defining a method:

func String() string

and associating it with the type, by declaring the type as the method's receiver. It is said that the method defines the "native" format for that type.

The String() method's intention is to read the receiver's state and render it, never to modify it. In consequence, the String() should declare a value receiver.

func (t SomeType) String() string {
   return fmt.Stringf("...", "...")
}

Thanks to compiler's facilities, String() can be invoked with both values and pointers:

type SomeType struct {
	s string
}

func (s SomeType) String() string {
	return "<" + s.s + ">"
}


...
t := SomeType{s: "A"}

fmt.Printf("%v\n", t)  // prints <A>
fmt.Printf("%v\n", &t) // prints <A>
fmt.Printf("%s\n", t)  // prints <A>
fmt.Printf("%s\n", &t) // prints <A>

Since we use a value receiver, the implementation does not have to protect against the receiver being nil.

fmt package printing functions automatically invoke the String() method, if defined, on instances of the type when the "%s" and "%v" conversion characters are used.

The fmt.Stringer Interface

https://pkg.go.dev/fmt#Stringer

The fmt.Stringer interface declares String() string as its only method:

type Stringer interface {
	String() string
}

fmt.Stringer is implemented by any type that has a String() method, The String() method is used to print values passed as an operand to any format that accepts a string or to an unformatted printer such as Print.

Recurrence Warning

Do not write a String() method that calls Sprintf() in way that will recur into your own String() method indefinitely. This can happen if the Sprintf() call attempts to print the receiver directly as a string with "%v" or "%s", which in turn will invoke the String() method again:

// Don't do this:
func (s SomeType) String() string {
     return fmt.Sprintf("SomeType<%v>", s)
}

IDEs may detect this anti-pattern via static checking and warn that "Placeholder argument causes a recursive call to the 'String' method (%v)".

A solution is to convert the argument to the basic string type with string(s), which does not have this method.