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
When converting C code to Go, it is easy to introduce the following mistake because in
Go, "case" don't fallthrough:
---- C code ----
switch (grade) {
case 'A' :
printf("Excellent !\n" );
break;
case 'B' :
case 'C' :
printf("Not bad !\n" );
break;
default :
printf("Try again !\n" );
}
---- Go code ----
switch grade {
case 'A' :
fmt.Println("Excellent !" );
case 'B' : <======= BUG INTRODUCED HERE
case 'C' :
fmt.Println("Not bad !" );
default :
fmt.Println("Try again !" );
}
It is easy to forget to merge "case" clauses, as the above should be:
case 'B',
'C' :
fmt.Println("Not bad !" );
It would be nice if vet could detect such consecutive CASE clauses, which are often
mistakes.
Other easier idea is to make "go fmt" write a semicolon in a line following an
empty CASE clause, just to make emtpy clauses more visible:
case 'B' :
;
case 'C' :
fmt.Println("Not bad !" );
The text was updated successfully, but these errors were encountered:
I agree that this is an easy mistake when converting from C to Go, but it's normal in Go
to write a case clause with no statements if there is nothing to do. We don't want to
change that in go vet or go fmt.
by nicolas.riesch:
The text was updated successfully, but these errors were encountered: