Weekdays with iota
An enumeration in Go is a named integer type plus a block of constants. There
is no enum keyword, and none is needed: iota and a defined type do the job.
iota is a counter that resets to 0 at the start of every const block and
increases by one per line. Inside a block, a constant line with no expression
repeats the previous one, so this declares three constants and not one:
type Level int
const (
Debug Level = iota // 0
Info // 1
Warn // 2
)
Giving the type a String() string method makes it print as a name instead of
a number, everywhere: fmt looks for that method on any value it formats.
Task
Finish main.go.
- Replace the placeholder constants with one
iotablock declaringSundaythroughSaturdayasWeekday, numbered0to6. String() stringreturns the day's English name. A value outside0..6is not a day, and formats asUnknown(<n>):Weekday(9).String()is"Unknown(9)", andWeekday(-1)is"Unknown(-1)".IsWeekend() boolis true for Saturday and Sunday only.Next() Weekdayreturns the following day and wrapsSaturdayback toSunday. A value outside0..6has no following day and comes back unchanged.
The tests check the numbers, the names, the wrap, and that fmt.Sprintf("%v", Friday) prints Friday rather than 5.
Hints
-
Sunday Weekday = iotaon the first line is what gives the whole block its type. The lines after it need no expression at all. -
A method is a function with a receiver in front of the name:
func (d Weekday) IsWeekend() bool { ... }dis the value the method was called on, exactly likethiselsewhere, and you pick its name. You will meet receivers properly in the methods concept; here you only need this one form. -
The names are easiest as a package-level array indexed by the day:
var dayNames = [...]string{"Sunday", "Monday", ...}[...]sizes the array from the literal. Check the range before you index it:dayNames[9]panics, and the tests passWeekday(9)on purpose. -
Write
int(d)when formatting the unknown case.fmt.Sprintf("Unknown(%d)", d)would callStringagain and recurse until the stack runs out. That is the classicStringertrap, and worth meeting once. -
d + 1is aWeekday, not anint: arithmetic on a defined type stays in that type. That is exactly why the enum is a type of its own rather than a bareint.