{

Golang Tutorials - 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, In this blog post, You will learn about Different types of comments and how to write comments in Go Language?. Go Language Comments

In a programming language, Comments are useful statements to describe the line of code statements. These are part of the program that contains statements used to place human-readable information. This will be helpful for developers to maintain the code, improve code quality, and Easily find bugs and fixes for it.

How do you write a comment in golang?

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 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 end of the code.

Following is the Single-line comments Syntax 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 places two single-line comments, // Main function - These comments placed before function declaration // Print Text to console - These comments place at 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
}

Writing Block or Multi line comments in Golang

Block comments are comment statements that 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 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 use 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")

}

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.

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 lines of comments in golang and help developers to have clean code. how to write documentation comments to help API users to understand the code written by other developers.

Reference

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.