GoLang - Comments

2 minute read

In this tutorial, you will learn about Comments in Go Programming with examples.

In Go programming, comments play a crucial role in making your code more readable, understandable, and maintainable. They are snippets of text within your code that are ignored by the compiler or interpreter, serving as notes to explain the purpose, functionality, or logic behind different parts of your program. In this tutorial, we will explore how to write comments in Go programming, along with examples, using simple and easy-to-understand language. So let’s get started!

Types of Comments

Go programming offers two types of comments: single-line comments and multi-line comments.

1. Single-Line Comments

Single-line comments are used to add explanatory or descriptive notes on a single line. The Go compiler ignores everything after the comment symbol until the end of the line. Example:

package main

import "fmt"

func main() {
    // This is a single-line comment
    fmt.Println("Hello, World!")

In the example above, the single-line comment begins with '//' and provides a brief explanation of the line of code that follows it.

2. Multi-Line Comments

Multi-line comments, also known as block comments, allow you to add comments that span multiple lines. They are typically used to provide detailed explanations or to temporarily disable blocks of code. Example:

package main

import "fmt"

func main() {
    /* This is a multi-line comment.
    It can span multiple lines.

    fmt.Println("Hello, World!")

In this example, the multi-line comment starts with '/*' and ends with '*/'. All the text enclosed within these symbols is considered a comment by the Go compiler.

Best Practices for Using Comments

  1. Use comments to explain the purpose and logic of your code, especially in complex or hard-to-understand sections.

  2. Write comments that are concise, clear, and easy to understand for both yourself and others who might read your code.

  3. Avoid redundant or unnecessary comments that restate obvious things.

  4. Update or remove comments as you modify your code to keep them in sync with the actual code.

  5. Use comments to document your public functions, explaining the parameters, return values, and any important details about their usage.


Comments in Go programming are essential for making your code more readable and understandable. They provide explanations, clarifications, and notes about the purpose, functionality, and logic of your code. In this tutorial, we explored two types of comments: single-line comments and multi-line comments. By following the best practices mentioned, you can effectively use comments to improve the maintainability of your Go programs. So go ahead, start incorporating comments into your code, and make it more informative and accessible to yourself and others!