Comments in the Go program

Comments in the Go program:

Comments are the non-executable lines. In other words, comments are those lines or statements in the program which will not be executed during the execution process. The compiler ignores the comments automatically in the program. Comments are very useful to the developer while he writes the code in the program. It is a part of each program. Comments give details about the code in the program.

If the developer wants to give some messages, then he uses comments in the program.

It gives the details about the source code, which are readable by a normal human. This information also helps in maintaining the code in the program. It also helps in debugging the bugs.

In the go language, comments are used to provide the developer details about the variable, functions control structures, and any specific line of code or statement in the program. This information is very useful in the maintenance of the codes.

Features of the comments:

There are many features of comments; these help the developer in writing the source code. Few features of the comments are given below:

  1. It is a non-executable statement in the program.
  2. The compiler and interpreter both ignore the comments in the program during the compilation process.
  3. It gives information about the variables and functions which are used in the program.
  4. It helps the developer to find out the error in the program.
  5. It helps the developer in clean coding.
  6. Comments are readable by a normal human, which have a basic knowledge of computer.

Types of comment:

There are three types of comments in the go language, which are given below:

  1. Single line comment
  2. Multiple line comment
  3. Documentation comment

Single line comment:

It is very clear by its name as this comment is for a single line.

When the developer writes the comment in a single line, it is known as a single-line comment. The single-line comments always start with double forward slashes, i.e.,//.

When you put double forward slashes (//) in front of any line or statement in the go program, then this statement will convert in the comment. These comments are used to describe a specific line.

You can put double forward slashes (//) anywhere in the program. Where you put these slashes, codes, or words will be covert into the comment from that place to the end of the line.

Syntax:

            // comment here;    

Example:

Here, the basic example of printing a string in go language with the single line comment is given below:

 package main
 // here is the single-line comment.
 import "fmt"
 func main() {
             //it is a program to print a string.
             fmt.Println("Hello.. This program is about single line comment.")
 // It is also a single line comment.
 } 

Output:

Comments in the Go program

Multiple line comment:

It is also clear by its name that this comment will be in multiple lines. It is also known as GoLang block comments or multi-line comments. If your comment exists in more than one line in the program, it is known as multiple line comments.

In multiple line comments, the comments always start with a single forward slash followed by a star sign like  /* and end with a star sign followed by a single forward slash like */. The compiler or interpreter will ignore the content written in between these symbols.

The developer uses these comments when he needs to provide some information about the codes in the program. You can also use multiple line comments anywhere in the program.

Note:

Although there is a different syntax for writing the multi-line comments, the developer can also write multiple line comments with single-line comments. If he uses single-line comment syntax to write multiple line comment, then every line of the comment will start with double forward slashes (//).

But making the multi-line comments in this way is foolish to work. Adding a double forward slash in front of each line is not the correct way and a waste of time; it is unnecessary to add the slashes in each row if you have a better way to do the same.

Syntax:

             /*comment here;
             It is multiple line comment.*/         

Example:

Here, the basic example of printing a string in go language with the multiple line comment is given below:

 package main
 /* It is a multiple line comment; we are also going to use multiple line comments
 inside the main function.*/
 import "fmt"
 func main() {
             /* It is a program about multiline comment.
             Here, we are discussing in the comment. Print the following text on the screen*/
  ft.Println("Hi..!! This program is about multiple line comment.") 
}

Output:

Comments in the Go program

Documentation comment:

It is a comment that is used by the developer at a higher level, basically in the development of the projects. It is used to provide some basic information to the user regarding the use of the application.

This comment creates the documentation API for the codes in go language. To create this API in the project, you require the go doc tool. The “go doc” tool generates a go documentation API; later, this API is used to provide the documentation comment to the user in the form of an HTML page. The documentation comment includes the HTML mark up and the texts on the page. 

Go doc tool provides a web server to the user. To start a web server, first of all, you need to run the following command:

                           go doc –http=:8331

8331 is just a port number; you can also change this port number according to your requirements. After running the command, you can open the server on your browser by running the URL “localhost:8331” in your URLs bar.

Single line and multiple line comments are used in each program to provide information to the developer about the codes. In contrast, the developer uses documentation comments to provide the information to the user as a reference; it helps API users understand the codes written by another developer.



ADVERTISEMENT
ADVERTISEMENT