A line comment begins with two forward slashes:
Go code
// Display a greeting
fmt.Println("Hello")Line-by-line explanation
- The first line begins with
//, so it is a comment. - The compiler ignores the comment text.
- The second line displays Hello.
Expected output
HelloThe 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
- The first line is a block comment.
- The markers contain the human explanation.
- The second line displays Ready.
Expected output
ReadyOnly 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
- The print instruction is complete.
- 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
- Begin with two forward slashes.
- Write plain English after them.
- Do not add quote marks around the comment.
Show Solution
Solution code
// Welcome the learner
fmt.Println("Welcome")Line-by-line explanation
- The first line is ignored by the compiler.
- The second line displays Welcome.
Expected output
WelcomeOnly the program instruction appears as output.