Golang Comments

In this tutorial, we are going to learn about the comments in Go Language, types of the Go comments with examples.
Submitted by IncludeHelp, on April 03, 2021

The comments in any programming language are used to write the description (explanation) of the logic, function, class, etc. in the source code that should not be executed (or ignored by the compiler).

Similarly, the comments in Golang are used for the same and they are not executed by the compiler i.e., the comment section is ignored by the compiler while the execution.

Types of Go comments

There are two types of comments in Golang:

  1. Golang Single Line Comment
  2. Golang Multi-line Comment

1) Golang Single Line Comment

The single-line comment is created by the double forward-slash (//). Whenever we want to write any description or hide any line of the code, we use the double forward-slash to comment on it.

Syntax:

// text

Example of Golang Single Line Comment

//Golang program to print "Hello World".

package main
import "fmt"

func main() {
    //Declare a string type variable
    var var1 string 
    
    //Assign a string to the variable
    var1 = "Hello World"
    
    fmt.Println(var1)
}

Output:

Hello World

In the above code, following are the comments,

//Golang program to print "Hello World".
//Declare a string type variable
//Assign a string to the variable

2) Golang Multi-line Comment

The multi-line comment is also known as Block Comments, it is started by the /* and ends with the */. So, any piece of code or text enclosed with the /* and */ is considered as multi-line commented. Single line comments can be also be written within these characters (/*...*/).

Syntax:

/* Line 1 */

/* Line 1
Line 2
...
...
*/

Example of Golang Multi-line Comment

/* Golang program to
print "Hello World".
*/

package main

import "fmt"

func main() {
	/*Declare a string type variable*/
	var var1 string

	/*Assign a string to the variable*/
	var1 = "Hello World"

	fmt.Println(var1)
}

Output:

Hello World

In the above code, following are the comments,

/* Golang program to
print "Hello World".
*/
/*Declare a string type variable*/
/*Assign a string to the variable*/




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.