Go Comments

Su Suriya Ravichandran Updated 14 Sep 2026
2 min read ·Lesson 4 of 12

Comments are non-executable text in your source code, ignored by the compiler. They let you explain why code does something, document how functions should be used, and temporarily disable code while debugging — all without changing program behavior.

Go supports two comment styles: single-line and block comments.

Single-Line Comments

A single-line comment starts with // and runs to the end of that line.

package main

import "fmt"

// Entry point of the program
func main() {
    // Print a welcome message
    fmt.Println("Welcome to Go programming")
}

You can also place a comment at the end of a line of code:

package main

import "fmt"

func main() {
    message := "Go is simple and powerful"
    fmt.Println(message) // Output the message
}

Multi-Line (Block) Comments

Block comments begin with /* and end with */. Everything between the two markers is ignored, including line breaks, which makes them useful for longer explanations or for disabling several lines of code at once.

package main

import "fmt"

func main() {
    /*
       This program demonstrates how multi-line
       comments work in Go. The message below
       is printed to the console.
    */
    fmt.Println("Understanding Go comments")
}

Block comments cannot be nested — writing /* /* ... */ */ will end the comment at the first */, leaving the trailing */ as a syntax error.

Commenting Out Code

Both comment styles are commonly used to temporarily disable a line or block of code while testing, without deleting it:

package main

import "fmt"

func main() {
    fmt.Println("This line will execute")

    // fmt.Println("This line is temporarily disabled")
}

This is a normal part of debugging, but commented-out code left behind in a finished program is generally considered clutter — it's best removed once you're done, since version control (like Git) already preserves the history if you need it back.

Documentation Comments

Go treats a comment placed directly above a function, type, or package declaration — with no blank line in between — as that item's official documentation. By convention, the comment starts with the name of the thing it documents:

// calculateArea returns the area of a rectangle
// given its length and width.
func calculateArea(length, width int) int {
    return length * width
}

These "doc comments" are not just a style convention — they are read directly by Go's tooling. Running go doc in a package, or viewing a package on pkg.go.dev, displays these comments as the function's official documentation. This is a key difference from comments in many other languages: in Go, writing a well-placed comment above an exported function is how you write API documentation, with no separate documentation syntax to learn.

Common mistake: leaving a blank line between the comment and the declaration breaks this connection — the comment is then treated as an ordinary comment, not documentation, and go doc will not pick it up.

0 Comments

Reviewed before they appear

No comments yet.

Go Lang
Ask about this post
AI Ask about this post

Ask questions about Go Comments and get answers drawn from it.

Signed-in readers only.