I am new to Go
, and while attempting to remove some duplicate code across switch statements I added a case with fallthrough
like so:
i := 1
switch i {
case 0, 1:
fmt.Println("common code")
fallthrough
case 0:
fmt.Println("aux for 0")
case 1:
fmt.Println("aux for 1")
default:
fmt.Println("other number")
}
However, I received an error about the duplicate cases such as:
prog.go:13: duplicate case 0 in switch
previous case at prog.go:10
prog.go:15: duplicate case 1 in switch
previous case at prog.go:10
Why is this an error? Is there some way to instruct the compiler to allow such code?