{
) of any explicit code block on a new line. For example, the following for
loop code block fails to compile.
for i := 5; i > 0; i--
{ // unexpected newline, expecting { after for clause
}
for i := 5; i > 0; i-- {
}
for
loop block compiles okay.
for
{
// do something ...
}
;
as terminators of code lines. However, we seldom use semicolons in our Go code. Why? The reason is most semicolons are optional and can be omitted. Go compilers will insert the omitted semicolons for us automatically in compiling.
package main;
import "fmt";
func main() {
var (
i int;
sum int;
);
for i < 6 {
sum += i;
i++;
};
fmt.Println(sum);
};
semicolons.go
, we can run go fmt semicolons.go
to remove all the unnecessary semicolons from that file. Compilers will insert the removed semicolons back (in memory) automatically in compiling the source code.
break
, continue
, fallthrough
, or return
++
, --
, )
, ]
, or }
)
or }
.
)
and the last semicolon within a code block or a (struct or interface) type declaration before the closing sign }
are optional. If the last semicolon is absent, compilers will automatically insert it back.
import (_ "math"; "fmt")
var (a int; b string)
const (M = iota; N)
type (MyInt int; T struct{x bool; y int32})
type I interface{m1(int) int; m2() string}
func f() {print("a"); panic(nil)}
var (a int; b string;);
const (M = iota; N;);
type (MyInt int; T struct{x bool; y int32;};);
type I interface{m1(int) int; m2() string;};
func f() {print("a"); panic(nil);};
var a = 1; var b = true
a++; b = !b
print(a); print(b)
for
keyword. This is why the bare for
loop example shown above is valid.
func f() {
a := 0
// The following two lines both fail to compile.
println(a++) // unexpected ++, expecting comma or )
println(a--) // unexpected --, expecting comma or )
}
func f() {
a := 0;
println(a++;);
println(a--;);
}
.
in a selector. We can only break a line after the dot, as the following code shows
anObject.
MethodA().
MethodB().
MethodC()
anObject
.MethodA()
.MethodB()
.MethodC()
anObject;
.MethodA();
.MethodB();
.MethodC();
package main
import "fmt"
func alwaysFalse() bool {return false}
func main() {
for
i := 0
i < 6
i++ {
// use i ...
}
if x := alwaysFalse()
!x {
// do something ...
}
switch alwaysFalse()
{
case true: fmt.Println("true")
case false: fmt.Println("false")
}
}
switch-case
block in the above example will print a true
instead of a false
. It is different from
switch alwaysFalse() {
case true: fmt.Println("true")
case false: fmt.Println("false")
}
go fmt
command to format the former one, a semicolon will be appended automatically after the alwaysFalse()
call, so it will become to
switch alwaysFalse();
{
case true: fmt.Println("true")
case false: fmt.Println("false")
}
switch alwaysFalse(); true {
case true: fmt.Println("true")
case false: fmt.Println("false")
}
true
.
go fmt
and go vet
often for your code.
func f(x int) {
switch x {
case 1:
{
goto A
A: // compiles okay
}
case 2:
goto B
B: // syntax error: missing statement after label
case 0:
goto C
C: // compiles okay
}
}
B:
label declaration invalid? The reason is, by the second semicolon insertion rule mentioned above, compilers will insert a semicolon before each of the }
characters following the A:
and C:
label declarations. As the following code shows.
func f(x int) {
switch x {
case 1:
{
goto A
A:
;} // a semicolon is inserted here
case 2:
goto B
B: // syntax error: missing statement after label
case 0:
goto C
C:
;} // a semicolon is inserted here
}
A:
and C:
label declarations are both valid. On the other hand, the B:
label declaration is followed by case 0:
, which is not a statement, so the B:
label declaration is invalid.
B:
label declaration to make it compile okay.
,
) Will Not Be Inserted Automatically
func f1(a int, b string,) (x bool, y int,) {
return true, 789
}
var f2 func (a int, b string) (x bool, y int)
var f3 func (a int, b string, // the last comma is required
) (x bool, y int, // the last comma is required
)
var _ = []int{2, 3, 5, 7, 9,} // the last comma is optional
var _ = []int{2, 3, 5, 7, 9, // the last comma is required
}
var _ = []int{2, 3, 5, 7, 9}
var _, _ = f1(123, "Go",) // the last comma is optional
var _, _ = f1(123, "Go", // the last comma is required
)
var _, _ = f1(123, "Go")
// The same for explicit conversions.
var _ = string(65,) // the last comma is optional
var _ = string(65, // the last comma is required
)
unexpected newline
syntax errors.
func f1(a int, b string,) (x bool, y int // error
) {
return true, 789
}
var _ = []int{2, 3, 5, 7 // error: unexpected newline
}
var _, _ = f1(123, "Go" // error: unexpected newline
)
break
, continue
and return
, or after any of the three keywords which are not followed by labels or return results;
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.
reflect
standard package.sync
standard package.sync/atomic
standard package.