Golang Tutorials - Comments in Go Language


Go Language Comments

When you are learning new language, The basic thing to learn is about data types ,Control flow statements and Comments, In this blog post, You will learn about Different types of comments and how to write comments in Go Language?.

In a programming language, Comments are useful statements to describe about line of code statements. Comments are part of the program that contains statements used to place human-readable information. This will be helpful for developers to maintain the code, improves code quality and Easy to find bug an d fixes for it.

In Golang, The comments are used to describe information about variables, control structures, functions or any line of code. Comments are ignored by the compiler when the program is compiled.

Important points aboug comments in Golang

  • Comments are useful for developer for code m aintenance
  • These are ignored by compiler and interpreter
  • These are line statements defined for variable, variable, method, conditional blocks or any line of code.
  • documentation annotations for code

Go Language provides the following types of comments

  • Single Line Comments
  • Multi Line Comments - Block comments
  • Documentation comments

Golang Single line Comments

Single-line Comment always prefixed with two forward slashes(\\), used to comment a single line of the statement. Single line comments are used to describe the line of code, can be placed before the line of code or end of the code.

Following is the syntax of Single-line comments Syntax

// Comments here(Statement of a line are comments)

Golang Compiler ignores any text or statements between // and end of the line. The below examples places two single-line comments, // Main function - These comments placed before function declaration // Print Text to console - These comments placed at end of the line of code. Following is an example of single-line comments

package main

import (
 "fmt"
)
// Main function
func main() {
 fmt.Println("Hello World")  // Print Text to console
}

Golang Block comments

Block comments are comment statements are placed in multiple lines, Therefore it is also called multi line comments.

We can write multi line comments with single line syntax, But each line starts with // symbole, and it cause uncessary adding // sybmole to each line. Therefore, multi-line comments are useful and helpful for developers to have a clean code. Multi-line comments are used to place comments/statements in multiple lines.

Here is the syntax of multi-line comments Syntax

/* This
is a multi line
comments */

Comments are started /* and end with */. Golang compiler ignores statements or text in between /* and */. Here is an example for Multi-line comments in Golang

package main

/* This program example prints
Hello world text to console
*/
import (
 "fmt"
)

// Main function
func main() {
 // Print Text to console
 fmt.Println("Hello World")

}

Golang Documentation Comments

Documentation comments are used to create Documentation API for the Golang code. These comments are used for packages or projects to generate documentation HTML pages, can be used by users for reference. The comments contain HTML markup and texts. you need to use Godoc tooll to create a Go documentation API.

package first

/* This program example prints
Hello world text to console
*/
import (
 "fmt"
)

/* Adding two numbers
return sum of numbers
*/
func sum(number1 int, number2 int) int {
 return number1 + number2
}

// Main function - entry point
func main() {
 // Print Text to console
 fmt.Println("Hello World")

}

Godoc provides a web server. Please run the below command to start a web server

godoc -http=:8331

and open localhost:8331 in a browser.

Conclusion

In conclusion, We can write single and multiple line of comments and helps developers to have clean code. And also learned how to write a documentation comments helps API users to understand the code written by other developers.

Reference

Similar Posts