You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here is a nice trick to write an enum-like type's constant values:
g% cat x.go
package p
type T int
const (
_ T = iota
T1
T2
T3
T4
)
g%
This has two properties I wanted: (1) the values start at 1, so the zero value is not a
defined value, and (2) all the defined values have the same form on the line, so that I
can pipe them through 'sort' without breaking anything. Compare to
const (
X T = 1 + iota
Y
Z
)
Anyway, this _ trick works well for me.
It works less well for godoc:
g% godoc .
PACKAGE DOCUMENTATION
package p
import "."
CONSTANTS
const (
T1
T2
T3
T4
)
TYPES
type T int
g%
It's a bit confusing that you can't see the _ in the godoc output, since the _ is what
defines the type and value of the constants.
Worse, the constants are not attached to the type T like they normally would be:
g% godoc . T
type T int
g%
If I rename the _ to Dummy, then it works:
g% godoc . T
type T int
const (
Dummy T = iota
T1
T2
T3
T4
)
g%
What's happening here is that _ is treated as unexported and so it filters away.
(Renaming to dummy shows the same behavior as _.)
Perhaps the best fix would be to treat _ as exported for the purposes of godoc filtering
in const lists and possibly also struct fields (where _ is often padding).
I thought about saying that _ should be treated as exported for all filtering, but you
probably don't want to show things like
func _() { ... }
var _ Interface = (*Type)(nil)
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: