Single-Line Comments in Go
To generate a single-line comment in Go, we use two forward slashes //.
Example
package main
import ("fmt")
func main() {
// declaring a variable
num := 50
// printing the variable
fmt.Println("Number is", num)
}
Output:
Number is 50
In the preceding example, we created a variable called num and printed it. //declaring a variable and //printing the variable are two single-line comments in this case.
Inline Comments in Go
Inline comments appear on the same line as the code in a statement. They, like other comments, begin with a pair of forward slashes.
[code] // Inline comment regarding the code
Example
num:= 10 // Initialize num with an arbitrary number
Multiline Comments in Go
A multiline comment in Go begins with /* and closes with */.
Example
package main
import ("fmt")
func main() {
/* creates a variable
to store the Roll Number of a student
*/
roll := 123456
fmt.Println("Roll Number:",roll)
}
Output:
Roll Number:123456
In the above example,
/* creates a variable
to store the Roll Number of a student
*/ is a multiline comment.
Because they span multiple lines, multiline comments are sometimes known as block comments.
Commenting Out Code for Testing
In addition to using comments to document code, you can also make a block comment by using opening tags (/*) and ending tags (*/). This lets you comment out the code that you don't want to run when testing or debugging a program you're working on. That is, if you have errors after implementing new lines of code, you may wish to comment out a few of them to see if you can isolate the problem.
Example
// Function to add two numbers
func addTwoNums(a, b int) int {
sum := a + b
return sum
}
// Function to multiply two numbers
func multiplyTwoNums(a, b int) int {
product := a * b
return product
}
func main() {
/*
In this example, we're commenting out the addTwoNums function because it's
failing and hence can not be executed. Only the multiplyTwoNums function will
be executed.
x := addTwoNums(9, 7)
fmt.Println(x)
*/
m := multiplyTwoNums(9, 7)
fmt.Println(m)
}
Commenting out code with the /* and */ tags allows you to experiment with alternative programming methodologies while also assisting you in locating the source of a problem by systematically commenting out and running sections of a program.
Frequently Asked Questions
-
What are comments in Go?
Comments are used in Go programming to increase the readability of the program. The Go compiler ignores the comments and hence does not compile or translate them into low-level code.
-
How do you comment in Go?
Comments in Go begin with a pair of forward slashes (//) and continue to the end of the line. It is customary to leave a space following the set of forward slashes.
Key Takeaways
In this article, we have extensively discussed comments and their implementation in Go. We learned about the importance of comments in a code, and further, we learned to write various types of comments in the Go programming language.
We hope that this blog has helped you enhance your knowledge regarding Golang and if you would like to learn more, check out our articles on Golang Archives . Do upvote our blog to help other ninjas grow. Happy Coding!”
Explore many more courses from coding ninjas and build your skills.