Go Comments

Learn via video courses
Topics Covered

Overview

When you write a piece of code for future use, you may wish to attach a note to it so that when you read it later, you remember why you wrote it. The goal of writing comments is sometimes to make it easier for others to understand what your code is doing. In this article, we'll see all about Comments in golang and how to use it with our code structure.

Introduction

Comments are used to improve the readability of the program. It is used to document something specific to the program. It can also be used to provide information, a description, or an explanation about the program's source code or any statements utilized in it. Comments contain human-readable data. Since the Go compiler ignores comments, they are not compiled or translated into low-level code.

We have two types of comments i.e single-line comments or multi-line comments in Go programming.

Most programmers use multiline comments because it's very beneficial in a large codebase for new contributors or developers to understand by just reading the comments. Even I also found it very useful if I have to understand any large codebase I first move on to the comment descriptions by the senior developers.

Benefits of Comments:

  • The main benefits of utilizing correct comments in programs are that they improve the quality and efficiency of the source code.
  • Generic comments improve the readability of the program's commands and logic.
  • The use of comments correctly can help the developer in code maintenance.
  • It helps us to identify errors faster.

Types of Comment

Go provides two types of comments, they are:

Single Line Comments:

Syntax:

Multi Line Comments:

Syntax:

Single Line

  • Single-line comments are used to provide feedback or description on a single line of code.
  • (//) slash is used to denote the single line comments for writing description. The compiler ignores anything put after // at the end of the line, therefore this content won't be executed.
  • A single-line comment can be put anywhere before or after the line of code.

For Example:

Output:

Try by Yourself!

Inline Comments

Inline comments appear on the same line as the statement's code. They, like other comments, start with a series of forwarding slashes. Again, no whitespace is required after the forward slashes, but it is considered conventional to do so that's why we follow.

Syntax:

Example:

Inline comments can also be used to clarify why you're doing something or to provide further information, as in:

Multi-Line

Block Comments

When we need to describe a more complex piece of code, we use block comments. In Go, we can write block comments in one of two methods.

The first method involves writing and repeating double-forward slash comments.

For example,

The multi-line comment format can also be used for the single-line comments example mentioned above.

Another approach is Multiline Comments.

Multiline Comments

  • Multi-line comments are sometimes known as block comments.
  • They are used in the software to comment on numerous lines of statements. Multi-line comments start with /* and end with */.
  • The text/statements/codes between /* and */ will not be processed during program execution.

For Example:

Output:

Try by Yourself!

Comments for Debugging Code

  • In many programming languages, developers use the comment feature to block the source code from being run. Commenting can be done on a single line of source code or an entire block of source code.
  • This functionality will help programmers in identifying errors in the code more efficiently which is given by the Go programming language.

For Example:

Output:

Try by Yourself!

Conclusion

  • In this article, we saw, that adding comments to your Go programs makes them more understandable for humans, especially your future self.
  • Adding relevant and valuable comments can make it easier for others to cooperate with you on programming projects and highlight the usefulness of your code.
  • And different types of comments we have in Golang.