Home > Article > Backend Development > There are several types of golang comments
Golang comments come in two forms: 1. Single-line comments (referred to as line comments), with the syntax "//single-line comments"; 2. Multi-line comments (referred to as block comments), starting with "/*" and It ends with "*/" and cannot be nested. The syntax is "/*comment content...*/". Developers can use single-line comments starting with "//" anywhere, while multi-line comments are generally used for package documentation or commenting out code snippets in blocks.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
Introduction to comments
The text used to annotate and explain the program is a comment, and comments improve the readability of the code;
The role of comments in the program is to annotate and explain the program to facilitate reading of the source code. The compilation system will automatically ignore the commented part when compiling the source code, so the comments will not play any role in realizing the function of the program. Appropriately adding comments to the source code can improve the readability of the source code.
Comments are good programming habits that a programmer must have. Organize your thoughts through comments first, and then use code to reflect them.
There are two forms of comments in Golang
Single-line comments are referred to as line comments and are the most common form of comments. , single-line comments starting with // can be used anywhere;
Multi-line comments are referred to as block comments, starting with /* and ending with */, and cannot be nested. , multi-line comments are generally used for documentation descriptions of packages or to comment code snippets into blocks. [Related recommendations: Go video tutorial, Programming teaching]
##1. Line comments
The format of single-line comments is as follows//单行注释Example:
2. Block comments (multi-line comments)
The format of multi-line comments is as follows/* 第一行注释 第二行注释 ... */Example: ##3. Description
Each package should have relevant comments. Add corresponding comments before using the package statement to declare the package name to briefly explain the function and role of the package.
At the same time, the comment content before the package statement will be considered as the documentation of this package by default. A package can be spread across multiple files, but only one of them needs to be commented.
You can use blank lines to separate multiple comments, as shown below:
// Package superman implements methods for saving the world. // // Experience has shown that a small number of procedures can prove // helpful when attempting to save the world. package superman
It is best to add corresponding comments to variables, constants, functions and other objects in the code. , which is conducive to later maintenance of the code, such as the comments on the enterOrbit function in the following code:
// enterOrbit causes Superman to fly into low Earth orbit, a position // that presents several possibilities for planet salvation. func enterOrbit() error { ... }4. Usage details
1) For line comments and blocks Comments, commented text will not be executed by the Go compiler.
2) Block comments are not allowed to be nested within quick comments.
For more programming related knowledge, please visit:
Programming VideoThe above is the detailed content of There are several types of golang comments. For more information, please follow other related articles on the PHP Chinese website!