From the GO language specification we know that the braces are required in if-else statement.
So it is not possible to write something like
if x
fmt.Println("hello")
In most other c-like languages braces can be omitted and if is applied only to the next following statement.
So what happens with the if-else-if statement? In other languages there are either separate statements elseif (php) elif (python) or just plain else if (c, java, etc) which is valid, because basically the second if clause is the next following statement (without braces).
it's ok to use either
if (x)
//...
else if {
//...
}
or
if (x)
//...
else {
if ...
}
tho the first one is much more common and preferred
And in go we can write
if x {
//...
} else if {
//...
}
but there are no braces after the else! so why is the second if is attached to the else statement with no braces surrounding it?
Aucun commentaire:
Enregistrer un commentaire