Closed as not planned
Description
Proposal Details
In addition to being able go from a type to a string
, it would be useful to parse a string
back into a type value.
I propose adding a new -scanner
option that would tell the stringer tool to also generate a fmt.Scanner
implementation that accepts the "%s"
verb. It makes sense to include this as part of x/tools/cmd/stringer because the implmentation can make use of the generated _Type_name
and _Type_index
array.
An example of use:
import "fmt"
type MyType uint64
const (
One MyType = iota
Two
Three
)
//go:generate stringer -type=MyType -scanner
func Example(v MyType) {
stringerOutput := fmt.Sprintf("<%s>", v)
var v2 MyType
_, err := fmt.Sscanf(stringerOutput, "<%s>", &v2)
if err != nil {
panic(err)
}
if v2 != v {
panic("the values should match")
}
}
I have an implementation that I have used in a private project, and I would like to contribute it back to the Go project. This proposal would provide a solution for the earlier request #60228.