In php exists a __toString()
method that allow to make a taylored representation of an object. For example:
final class Foo
{
public function __toString()
{
return "custom representation";
}
}
$foo = new Foo();
echo $foo; // this will output "custom representation"
In Go it is possible to create a struct:
type Person struct {
surname string
name string
}
sensorario := Person{
"Senso",
"Rario",
}
fmt.Println(sensorario) // this will output "{Senso Rario}"
It is possible to add a toString method to the struct?
EDIT:
I've found this solution:
func (p *Person) toString() string {
return p.surname + " " + p.name
}
fmt.Println(simone.toString())
But what I am looking for, is the way to replace
fmt.Println(simone.toString())
with
fmt.Println(simone)