Comments in Go
A comment in programming is a programmer-readable explanation that is mainly written inside the source code of the program. Comments are ignored by the compilers and interpreters. They are generally written to make the program more readable and explanatory.
Sometimes when you might write a piece of code for later use, and you want to attach a note to the code so that when you open it later you remember the reason you wrote it for. The intention behind writing comments is sometimes also about that you want others to understand code easily what your code is doing.
In general, it is recommended to write comments in Go.
There are different types of comments that are present in Go. These mainly are:
Single-Line Comments
The syntax for a single-line comment in Go usually begins with a set of two forward slashes(//
) and anything that follows the two slashes will be ignored by the Go compiler. Let's make use of comments in a Go program and understand how to make use of them.
Consider the example shown below:
package main
import (
"fmt"
)
func main() {
// declaring a name vairable of type string
var name string = "Tuts"
fmt.Println(name)
}
In the above code example, we have a comment just after we enter the main function code block.
Tuts
Let's consider one more example, where we make use of comments. In the example below, we will write multiple comments. It means a program can have any number of comments.
Consider the example shown below:
package main
import (
"fmt"
)
func main() {
// declaring a slice of string type
var fruits = []string{"apple", "mango", "banana", "kiwi"}
// iterating over the slice
for i := 0; i < len(fruits); i++ {
fmt.Println(fruits[i])
}
}
It is recommended that the indentation of the comments should be the same as that of the code on the next line.
apple
mango
banana
kiwi
Block comments
Block comments are used when we want to explain a more complex code. There are two ways in which we can write block comments in Go.
The first approach includes writing double forward slashes comments and repeating them. For example,
// This is
// a block
// line comment
Another approach is to make use of a new syntax. The syntax involves /*
and */
as a way to mark a block or sometimes also known as a multi-line comment.
Consider the example shown below:
package main
import (
"fmt"
"time"
)
func main() {
// declaring a slice of string type
var fruits = []string{"apple", "mango", "banana", "kiwi"}
/*
The below code will start a new go routine
and make the code asynchronous in nature
*/
for i := 0; i < len(fruits); i++ {
go printFruit(fruits[i])
}
time.Sleep(1 * time.Second)
}
func printFruit(fruit string) {
fmt.Println("Current fruit is:", fruit)
}
The bock comment in the above code is used to explain a more complex code that is written just after it.
Current fruit is: mango
Current fruit is: kiwi
Current fruit is: apple
Current fruit is: banana
Conclusion
In the above tutorial, we learned what comments are, how to declare comments in Go, both single line and multiline comment, and when to use the comment.