Go · Stage 0 – Before You Code · Lesson 7 of 64

Comments

Add notes for humans that Go ignores while compiling and running the program.

A line comment begins with two forward slashes:

Go code

// Display a greeting
fmt.Println("Hello")
Line-by-line explanation
  1. The first line begins with //, so it is a comment.
  2. The compiler ignores the comment text.
  3. The second line displays Hello.
Expected output
Hello

The comment does not appear in the output.

Go also supports block comments beginning with /* and ending with */. Use them sparingly; ordinary line comments are usually easier to edit.

Go code

/* Explain an unusual decision. */
fmt.Println("Ready")
Line-by-line explanation
  1. The first line is a block comment.
  2. The markers contain the human explanation.
  3. The second line displays Ready.
Expected output
Ready

Only the real instruction produces output.