1

I am trying to understand this example from the Go Tour.
What is the significance of this last comma on line 3

fmt.Println(
    pow(3, 2, 10),
    pow(3, 3, 20),
)

How do line breaks generally modify the code in go.
I know, that without the line breaks, I can write this statement as

fmt.Println( pow(3, 2, 10), pow(3, 3, 20) )

and it would compile.
So, why is the extra comma needed with line breaks

John Smith
  • 31
  • 1
  • 4

2 Answers2

2

Go "automatically" adds ; as the end of a statement.

So

fmt.Println(
    pow(3, 2, 10),
    pow(3, 3, 20),
)

as the same as

fmt.Println(
    pow(3, 2, 10),
    pow(3, 3, 20),
);

But

fmt.Println(
    pow(3, 2, 10),
    pow(3, 3, 20)
)

is the same as

fmt.Println(
    pow(3, 2, 10),
    pow(3, 3, 20);
);

which is obvioulsy a syntax error.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

There's no significance. Trailing commas are allowed in function calls, although go fmt will remove them.

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118