Actually there's a way. But let's clear some things first.
In constant declarations, if the type is present, the constant will take the specified type:
const n int64 = 3 // n will be a typed constant, its type will be int64
If the type is omitted, the constant will take the type of the expression:
const x = int16(3) // x will be a typed constant, its type will be int16
If the expression is an untyped constant, the declared constant will remain untyped constant:
const i = 1 // i will be an untyped integer constant
Note that if you try to print i
's type (e.g. with fmt.Printf("%T", i)
, you will see int
, and that's because when passing a constant to a function or when assigning it to a variable, it has to be converted to an actual type, and the default type will be used (because fmt.Println()
has arguments type of interface{}
) - which is int
for an untyped integer constant.
Within a parenthesized const
declaration list the expression list may be omitted from declarations (except at the first). If the expression is missing, the previous non-empty expression will be used (textual substitution).
So when you do this:
const(
Stage1 FeeStage = iota
Stage2
Stage3
)
It means:
const (
Stage1 FeeStage = iota
Stage2 FeeStage = iota
Stage3 FeeStage = iota
)
Which results in 3 new constants: Stage1
, Stage2
and Stage3
, all being of type FreeStage
.
Your second example:
const (
Stage1 FeeStage = iota // 0
Stage2 = iota + 6 // 7
Stage3 = (iota - 3) * 5 // -5
)
Since you didn't omit the expressions, ONLY your first constant Stage1
will be a typed constant (of type FreeStage
), the rest will be untyped constants! So this doesn't even qualify (doesn't comply with your requirements)!
And now to your point: you want something like this:
const(
Stage1 FeeStage = 1
Stage2 = 2
Stage3 = 2
)
As stated above, if you leave out the type, Stage2
and Stage3
will be untyped constants. So type must be specified, you may take advantage of the fact that a const specification is:
ConstSpec = IdentifierList [ [ Type ] "=" ExpressionList ] .
You may specify an identifier list:
const(
Stage1, Stage2, Stage3 FeeStage = 1, 2, 2
)
Is this more readable? Maybe if there are a few constants only. If there are many, use Not_a_Golfer's recommendation:
const(
Stage1 FeeStage = 1
Stage2 FeeStage = 2
Stage3 FeeStage = 2
)