Go Comments

Comment lines are lines in computer programmes that compilers and interpreters disregard. Comments in programmes make code more legible for humans by providing information or explanations about what each component of a programme does.

Comments can act as notes to oneself or reminders, or they can be made with the objective of allowing other programmers to understand what your code is doing, depending on the purpose of your program.

In general, writing comments while developing or updating a program is a good idea because it’s easy to forget your thought process later on, and comments posted later may be less beneficial in the long run.

Comment Syntax

Comm Begin with a line of forward slashes (//) and work your way to the end. A white space after the forward slashes is considered idiomatic.

In general, comments will appear as follows:


// This is just a comment

Because comments are not executed, they will not be visible when a programme is run. The source code contains comments for humans to read, not for machines to execute.

Block Comments

Block comments can be used to clarify more sophisticated code or code that the reader is unlikely to understand.

In Go, there are two ways to make block comments. The first is to use a series of double forward slashes that you repeat for each line.


// First line of a block comment
// Second line of a block comment

The second option is to utilise opening (/*) and closing (*/) tags. It is considered customary to always use/syntax when documenting code. The /*… */ syntax is solely used for debugging, as we’ll see later in this article.


/* Everything inside 
this will be treated 
as block comment */

Inline Comments

Following the code, inline comments appear on the same line as the statement. They begin with a series of forward slashes, much like other comments. Again, there is no requirement for whitespace following the forward slashes, although it is regarded idiomatic.

Inline comments usually look like this:


[code] // Inline comment about the code

Inline comments should be used sparingly, but they can be useful for explaining complicated or non-obvious code. They can also come in handy if you suspect you won’t recall a line of code you’re creating in the future, or if you’re working with someone who isn’t familiar with all elements of the code.

Testing Code by Commenting It Out

You can use starting tags (/*) and ending tags (*/) to create a block comment in addition to utilising comments to document code. You can use this to comment out code that you don’t want to run when testing or debugging a programme you’re working on. That is, if you get issues after adding new lines of code, you could wish to comment out a few of them to see if you can isolate the problem.

Using the /* and */ tags allows you to experiment with different options while figuring out how to set up your code. You may also use block comments to comment out failing code while you work on anything else.


Leave a Reply

Your email address will not be published. Required fields are marked *