Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Go Comments
3.
Single-Line Comments in Go
3.1.
Example
4.
Inline Comments in Go
4.1.
Example
5.
Multiline Comments in Go
5.1.
Example
6.
Commenting Out Code for Testing
6.1.
Example
7.
Frequently Asked Questions
8.
Key Takeaways
Last Updated: Mar 27, 2024

Go Comments

Author Sanjana Yadav
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Comments are lines in computer programs that compilers and interpreters ignore. Comments in programs make code more understandable for humans by providing information or explanations about what each program element is doing.

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

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

Go Comments

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.

There are two types of comments in Go programming. The single-line will begin with //, while the general or multiple-line will begin with /* and end with */.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

  1. 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.

     
  2. 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.

Previous article
Go Loop Control Statement
Next article
Go Type Casting
Live masterclass