mirror of
https://github.com/golang/go
synced 2024-11-18 16:54:43 -07:00
The Go programming language
2c5c896732
This tool creates String methods from constant definitions. It's a time-saver designed to be used from go generate. The methods generated are efficient, more so than one is likely to create by hand. Given package date type Day int const ( Monday Day = iota Tuesday Wednesday Thursday Friday Saturday Sunday ) the command stringer -type Day will create the file day_string.go containing package date import "fmt" var ( _Day_indexes = []uint8{6, 13, 22, 30, 36, 44, 50} _Day_names = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday" ) func (i Day) String() string { if i < 0 || i >= Day(len(_Day_indexes)) { return fmt.Sprintf("Day(%d)", i) } hi := _Day_indexes[i] lo := uint8(0) if i > 0 { lo = _Day_indexes[i-1] } return _Day_names[lo:hi] } There are several strategies for the created method chosen according to the structure of the sequence of constants. Handles integer types only, both signed and unsigned. That's probably all that is needed. Tests to follow, but the test structure will be large so sending this out separately. The code has been heavily hand-tested but there are some bugs. Don't depend on this until the tests are installed. LGTM=gri R=gri, josharian CC=golang-codereviews https://golang.org/cl/136180043 |
||
---|---|---|
astutil | ||
blog | ||
cmd | ||
container/intsets | ||
cover | ||
dashboard | ||
go | ||
godoc | ||
imports | ||
oracle | ||
playground | ||
present | ||
refactor | ||
.hgignore | ||
AUTHORS | ||
codereview.cfg | ||
CONTRIBUTORS | ||
LICENSE | ||
PATENTS | ||
README |
This subrepository holds the source for various packages and tools that support the Go programming language. Some of the tools, godoc and vet for example, are included in binary Go distributions. Others, including the Go oracle and the test coverage tool, can be fetched with "go get". Packages include a type-checker for Go and an implementation of the Static Single Assignment form (SSA) representation for Go programs. To submit changes to this repository, see http://golang.org/doc/contribute.html.