Stringer
One of the important interfaces to keep in mind while writing Go code is the Stringer interface defined in the fmt package.
Go
type Stringer interface {
String() string
}Example using consts, the equivlant could be done using a dictionary but this is more idiomatic resulting in cleaner code on the user’s end.
Go
type hint byte
const (
absentCharacter hint = iota
wrongPosition
correctPosition
)
func (h hint) String() string {
switch h {
case absentCharacter:
return "."
case wrongPosition:
return "x"
case correctPosition:
return "O"
default: // shouldn't ever hit this
return ""
}
}