bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Go/Go Tutorial
Go•Go Tutorial

Go Comments

A comment is a text that is ignored upon execution.

Comments can be used to explain the code, and to make it more readable.

Comments can also be used to prevent code execution when testing an alternative code.

Go supports single-line or multi-line comments.

Go Single-line Comments

Single-line comments start with two forward slashes ( // ).

Any text between // and the end of the line is ignored by the compiler (will not be executed).

Example

// This is a comment package main import ("fmt") func main() { // This is a comment fmt.Println("Hello World!")
}

The following example uses a single-line comment at the end of a code line:

Example

package main
import ("fmt")
func main() {
  fmt.Println("Hello World!")
  // This is a comment
}

Go Multi-line Comments

Multi-line comments start with /* and ends with */ .

Any text between /* and */ will be ignored by the compiler:

Example

package main
import ("fmt")
func main() {
  /* The code below will print Hello World
  to the screen, and it is amazing */
  fmt.Println("Hello World!")
}

Tip

It is up to you which you want to use. Normally, we use // for short comments, and /* */ for longer comments.

Comment to Prevent Code Execution

You can also use comments to prevent the code from being executed.

The commented code can be saved for later reference and troubleshooting.

Example

package main
import ("fmt")
func main() {
  fmt.Println("Hello World!")
  // fmt.Println("This line does not execute")
}

Previous

Go Syntax

Next

Go Variables

This chapter

Overview
39

Lessons

95m

Read time

1. Go Tutorial2. Go Introduction3. Go Getting Started4. Go Syntax5. Go Comments6. Go Variables7. Go Multiple Variable Declaration8. Go Variable Naming Rules9. Go Constants10. Go Output Functions11. Go Formatting Verbs12. Go Data Types13. Go Boolean Data Type14. Go Integer Data Types15. Go Float Data Types16. Go String Data Type17. Go Arrays18. Go Slices19. Go Access, Change, Append and Copy Slices20. Go Operators21. Go Arithmetic Operators22. Go Assignment Operators23. Go Comparison Operators24. Go Logical Operators25. Go Bitwise Operators26. Go Conditions27. Go if statement28. Go if else Statement29. Go else if Statement30. Go Nested if Statement31. Go switch Statement32. Go Multi-case switch Statement33. Go For Loops34. Go Functions35. Go Function Parameters and Arguments36. Go Function Returns37. Go Recursion Functions38. Go Struct39. Go Maps

On this page

Go Single-line CommentsGo Multi-line CommentsComment to Prevent Code Execution