Syntax and Comments in Go Program


December 26, 2021, Learn eTutorial
1403

Comments in Go

Comments are the text in the program which are ignored during the program execution. The compiler considers a comment as a non-executable command. Comments are used for program documentation. It can provide information, description, or an explanation about the source codes or any statements used in the program. Comments are human-readable information.
Comments are also used to hide some codes for a specific time or to prevent the execution of certain codes or a block of codes in a program during the program testing.

The major benefits of using proper comments in the programs are:

  • It will improve the quality and efficiency of the source codes.
  • Writing good comments will increase the readability of the commands and logic used in the programs.
  • Proper use of comments can help the developer with code maintenance.
  • Comments are very helpful to find errors faster.

Go provides two forms of comments, they are:

Single-line Comments

Single-line comments are used to comment on a single line of statements in the program. They are used to describe a single line of code. 
Two forward slashes ( // ) are used for single-line comment. Anything written after // to the end of the line is ignored by the compiler which means that this text will not be executed.
A single-line comment can be placed before the line or at the end of the code.

Syntax of single-line comment:


// type the comments here

Example for single-line comment:


// This is a single-line comment before the line of code
package main
import ("fmt")// This is a single-line comment at the end of the code
func main() {
  fmt.Println("Hello World!") 
}  

Multi-line Comments

Multi-line comments are also known as block comments. They are used to comment on multiple lines of statements in the program. Multi-line comments start with /* and end with */. The text/statements/codes written between /* and */ will not be executed during the execution of the program.
Single-line comments can also be written in multi-line comment format. 

Syntax of Multi-line comment:


/* type multiple lines of statements here*/

Example for Multi-line comment:


package main
import ("fmt")
func main() {
  /* this is a multi-line comment
it contains multiple lines of comments */
  fmt.Println("Hello World!")
}

Source code as Comment

The comment feature is also widely used by programmers in many programming languages to prevent the source code from being executed. A single line of source code or a block of source code can be commented on. This feature will help the programmers to find errors in the program. Go language also supports this feature. 

Example


package main
import ("fmt")
func main() {
   //fmt.Println("Hello World!")
 /**/
}

Structure of a Basic Go Program

A Go program is mainly constructed using the following parts:

  1.     Package declaration
  2.     Import packages
  3.     Functions
  4.     Statements and expressions

To understand the structure of a basic Go program, an example program is given below:

Example program to display a message on the screen

GO : COMMENTS

Let’s learn each part of the Go program…

  1. Package declaration: 

    It is compulsory to start every Go program with a package declaration because every Go program is a part of a package. The packages are used to reuse and organize the code. In the Go programming language, the keyword "package" is used to define the packages. In the above example, the program belongs to the "main" package. The "main" package tells the compiler that the package must compile in the executable program. An executable program is a program that can run directly from the terminal. The “main” package also contains the main() function in it.

  2. Import packages:

    This part is used to import various packages into your Go program. Go programming language provides various built-in packages with pre-defined basic functionalities to help the programmers to ease the coding. The "fmt" (fmt stands for Format package) is one such package, and it is used to format input and output with functions. In the above example program, the files in the "fmt" package are included in this program.

  3. Functions:

    The execution of a Go program begins from the main() function. The main package contains the main() function, and every executable program in the Go programming language must contain a single main package and main() function. There is no need to call the main() function to execute the codes inside it because this function is invoked automatically. The main() function does not allow any arguments and does not return anything. In the Go programming language, the keyword "func" is used to create a function.

  4. Statements and expressions:    

    The statements and expressions of the program are written in this part. In the above example program, the statement fmt.Println("Hello World!") is used to display the "Hello World" string on the screen. Here, Println() is a method of the fmt package.