CodeBlanks Logo Icon CodeBlanks

Comments and semicolons

Learn // and /* */ comments and the semicolon rule in Go.

Comments

Go has two kinds of comments: single-line (//) and multi-line (/* ... */). The compiler ignores comments; they are used for explanation and notes.

A single-line comment runs from // to the end of the line. A multi-line comment starts with /* and ends with */; it can span several lines.

Good comments explain the intent; they say why, not what. Avoid comment clutter.

Automatic semicolons

The Go compiler automatically inserts semicolons at line ends. So most lines do not end with a semicolon. If you write one, it does not cause an error but Go tools will strip it.

You can write a semicolon by hand when really needed (e.g. multiple statements on one line). But Go style keeps one statement per line.

This automatic behavior works in harmony with gofmt; running gofmt -w keeps your code consistent.

Practice Add a comment and output

Add a // comment to the program and produce the output Go is great with fmt.Println.

Starter code

main.go
package main

import "fmt"

func main() {
    return
}

Sample solution

main.go
package main

import "fmt"

func main() {
    // Information
    fmt.Println("Go is great")
}

Expected output

Go harika

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which starts a single-line comment in Go?

  • #
  • --
  • // Correct answer
  • ;

Explanation: // starts a single-line comment in Go.

2 Single-line comment

Question
// Score ____ 90
Answer
// Score value: 90

Explanation: 'value:' is an explanatory note inside a comment.

3 Match the comment kinds

Term Meaning
// Single-line comment
/* */ Multi-line comment
gofmt Code formatter
comments Ignored by the compiler

All lessons in Beginner