How to write a Comments in Go Language

When you are learning a new language, The basic thing to learn is about data types, control flow statements, and Comments.

This post explains Different types of comments and how to write comments in Go Language?.

In a programming language, Comments contains statements to describe the line of code statements. These are part of the program code that contains statements used to place human-readable information. It helps developers to maintain the code readability, improve code quality, and easily find bugs and fixes for it.

What are Comments in Golang?

In Golang, The comments 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 about comments in Golang.

  • Comments are useful for a developer for code maintenance
  • These are ignored by the 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

How to write a Single line Comments in Go Language

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

Following is the Single-line comments Syntax:

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

Golang Compiler ignores any text or statements between // and the end of the line.

The below examples place two single-line comments.

// Main function - These comments declared before the function declaration
// Print Text to console - These comments placed at the end of the line of code.

Following is a single-line comments example.

package main

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

How to write Multi-line(Block) comments in Golang?

Block comments are comment statements , that contains comments 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 the // symbol, and it causes unnecessary adding the // symbol to each line. Therefore, multi-line comments are useful and helpful for developers to have clean code.

Multi-line comments are used to place comments/statements in multiple lines.

Here is the multi-line comments syntax.

Syntax:

/* This
is a multi-line
comments */

These Comments are started /\* and end with \*/. Golang compiler ignores statements or text in between /\* and \*/.

Here is a Multi-line comments example 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")

}

How to write Golang Documentation Comments?

Documentation comments are used to create a Documentation API for the Golang code. These comments are used for packages or project applications to generate documentation HTML pages, which can be used by users for reference by other developer users.

The comments contain HTML markup and texts. You need to use Godoc tool🔗l 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 lines of comments in golang and help developers have clean code. How to write documentation comments to help API users understand the code written by other developers.

Reference