No, Go does not support literal types (a la TypeScript). Moreover, it actually can't, because of zero values.
Every type has a zero value which always exists and is always assignable to it. When variables of a type are declared, they are implicitly assigned to their type's zero value.
For integer (int
, uint
, int32
, uint32
, int64
, uin64
) or float (float32
, float64
) or complex (complex64
or complex128
) types, this is just 0
(0.0
respectively).
For string types, this is the empty string ""
.
For slices, maps, pointers, channels, and interfaces, the zero value is nil
.
For arrays (not slices: arrays are value-types with statically-known length); their zero value is just the zero value of the element type repeated to fill every slot
The zero value of a struct type is a struct all of whose fields are zero values
In any case, because it is always possible for any type to have a zero value, it would be incompatible to create a type which only allows for any particular non-zero value.
The best a literal type "foo"
could possibly represent is that the value is either "foo"
or the zero value ""
(and no, Go doesn't support this anyway).
The closest you'll be able to do is a const
ant declaration, or a receiver function that just-so-happens to return a fixed value instead of an actual field.