Go Basics

Go Comments

Description

Comments are lines of text ignored by the compiler. They are used to explain code logic. Go supports C-style comments: single-line (`//`) and multi-line (`/* ... */`).

Pros & Cons

✅ Pros

  • Documentation: Essential for code maintainability.
  • Godoc: Special comments generate automatic documentation.

❌ Cons

  • None.

When to Use

Documenting public functions and complex logic.

Example

Using formatting for Godoc.

// Package main defines the entry point.
package main

import "fmt"

// main is the primary function.
func main() {
    // This prints text
    fmt.Println("Comments example")
}

Best Practices

Write comments for all exported (capitalized) functions. Start the comment with the function name.

← Go SyntaxGo Variables →