mirror of
https://github.com/golang/go
synced 2024-11-18 18:54:42 -07:00
63e6ed9258
When constant values change but stringer has not been run again, we can get misleading string values. Protect against this by generating code that will fail with a compiler error when this happens. Most compilers should be smart enough to omit the code containing the checks. Change-Id: I7a36d20f014cba0e7d88851d1b649a098ee30d76 Reviewed-on: https://go-review.googlesource.com/c/tools/+/163637 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
40 lines
690 B
Go
40 lines
690 B
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// This file is the same as day.go except the constants have different values.
|
|
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
type Day int
|
|
|
|
const (
|
|
Sunday Day = iota
|
|
Monday
|
|
Tuesday
|
|
Wednesday
|
|
Thursday
|
|
Friday
|
|
Saturday
|
|
)
|
|
|
|
func main() {
|
|
ck(Monday, "Monday")
|
|
ck(Tuesday, "Tuesday")
|
|
ck(Wednesday, "Wednesday")
|
|
ck(Thursday, "Thursday")
|
|
ck(Friday, "Friday")
|
|
ck(Saturday, "Saturday")
|
|
ck(Sunday, "Sunday")
|
|
ck(-127, "Day(-127)")
|
|
ck(127, "Day(127)")
|
|
}
|
|
|
|
func ck(day Day, str string) {
|
|
if fmt.Sprint(day) != str {
|
|
panic("day.go: " + str)
|
|
}
|
|
}
|