Friday, December 21, 2018

Golang Tutorials - Comments in Go Language

In any programming language, Comments are useful statements describes code statements. Comments are part of the program contains statements used to place human-readable information. This will be helpful for developers to maintain the code, improves code quality and easier bug fixing.
Golang Comments

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.
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 Multi-line comments-  Block Comments

To write multi-line comments using single-line comments, It will be very complex to write as each line starts with // So multi-line comments are useful for clean code. Multi-line comments are used to place comments/statement 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 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 tool 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.

Related article


EmoticonEmoticon