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.

Tiny Practice

Starter scenario

Add a line comment above the existing instruction saying it welcomes the learner.

Starter template

CHANGE_ME
fmt.Println("Welcome")
Line-by-line explanation
  1. The print instruction is complete.
  2. The first line needs a comment.
Expected output
(no output)

The starter is incomplete, so do not expect it to run successfully until you replace the missing part.

Hint
  1. Begin with two forward slashes.
  2. Write plain English after them.
  3. Do not add quote marks around the comment.
Show Solution

Solution code

// Welcome the learner
fmt.Println("Welcome")
Line-by-line explanation
  1. The first line is ignored by the compiler.
  2. The second line displays Welcome.
Expected output
Welcome

Only the program instruction appears as output.