Comments
Single line comments
Single line comments start with //
.
main.go
// This is a single line comment
Multi line comments
Multi line comments start with /*
and end with */
.
main.go
/*
This is a multi line comment
*/
Documentation comments
Documentation comments start with /**
and end with */
.
main.go
/**
This is a documentation comment
*/
Example
main.go
package main
import "fmt"
func main() {
// This is a single line comment
/*
This is a multi line comment
*/
/**
This is a documentation comment
*/
fmt.Println("Hello World")
}
output
Hello World
Explanation
- Single line comments are used to add comments to a single line.
- Multi line comments are used to add comments to multiple lines.
- Documentation comments are used to add comments to multiple lines. They are used to generate documentation for the program.
- Comments are ignored by the compiler and are not executed.
- Comments are used to explain the code and make it more readable.
- Comments are used to prevent execution when testing alternative code.
- Comments are used to prevent execution when debugging code.
Summary
In this chapter, you learned about comments in Go. In the next chapter, you will learn about variables in Go.