Go Comments
A comment is a note in code that the compiler ignores. Comments help people read and understand code.
A line comment
The comment sits on its own line. The print line below it still runs.
package main
import "fmt"
func main() {
// Print a greeting
fmt.Println("Hello")
}A block comment around a statement
The block comment covers one whole statement. That statement does not run.
package main
import "fmt"
func main() {
/* fmt.Println("Hidden") */
fmt.Println("Visible")
}A trailing comment
The comment follows code on the same line. The code still runs as usual.
package main
import "fmt"
func main() {
fmt.Println("Done") // Mark the end
}Example
package main
import "fmt"
func main() {
// Print the plan for today
fmt.Println("Plan for today")
fmt.Println("Write one program") // The second line of the plan
}Key points
- Use // to start a line comment.
- A line comment runs to the end of its line.
- Use /* and */ to start and end a block comment.
- A block comment can cover one statement or many lines.
- A trailing comment sits on the same line as code.
In this example, a line comment sits above the first print line. The first line prints Plan for today. A trailing comment sits after the second print line. The second line prints Write one program.
Note
The compiler skips every comment when it builds a program.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Go Playground
Exercise
Which marks the start of a line comment?