Go String(): Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 10: Line 10:
func String() string
func String() string
</syntaxhighlight>
</syntaxhighlight>
and associate it with the type, by declaring the type as the method receiver.
and associate it with the type, by declaring the type as the method receiver. It is said that the method defines the "native" format for that type.




Line 24: Line 24:
}
}
</syntaxhighlight>
</syntaxhighlight>
<code>Stringer</code> is implemented by any type that has a <code>String()</code> method, which defines the "native" format for that value. The <code>String()</code> method is used to print values passed as an operand to any format that accepts a string or to an unformatted printer such as <code>Print</code>.  
<code>Stringer</code> is implemented by any type that has a <code>String()</code> method, The <code>String()</code> method is used to print values passed as an operand to any format that accepts a string or to an unformatted printer such as <code>Print</code>.  


Java developers are used to declare a <code>public String toString() {...}</code> method in their class to get instances of that class rendered as custom Strings.
Java developers are used to declare a <code>public String toString() {...}</code> method in their class to get instances of that class rendered as custom Strings.

Revision as of 23:46, 30 August 2024

Internal

Overview

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

func String() string

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





type Stringer interface {
  String() string
}

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.

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. It consists in making the type that needs this behavior implement the fmt.Stringer interface, which has only one String() method that renders the "native" string representation for the instances of that type. The print() or fmt.*Print* library code will use the method if found on the instance to be rendered as string.

Note that is important whether we declare a value or a pointer receiver type. Depending on how the receiver type is declared, we will have to pass to the rendering code either a value, or a pointer to the instance of the type to achieve the desired behavior.

Example

type SomeType struct {
	ID string
}

// String makes SomeType implement the fmt.Stringer interface. It is important whether a value or a pointer receiver type is declared.
func (e SomeType) String() string {
	return fmt.Sprintf("SomeType[%s]", e.ID)
}

...

t := SomeType{ID: "001"}
fmt.Println(t)

will render:

SomeType[001]