Tutorials References Exercises Menu
Paid Courses

Go Comments


Go Comments

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

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

Comments can be single-lined or multi-lined.


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).

This example uses single-line comments:

Example

// This is a comment
package main
import ("fmt")

func main() {
  // This is a comment
  fmt.Println("Hello World!")
}
Try it Yourself »

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

Example

package main
import ("fmt")

func main() {
  fmt.Println("Hello World!") // This is a comment
}
Try it Yourself »

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 the words Hello World
  to the screen, and it is amazing */
  fmt.Println("Hello World!")
}
Try it Yourself »

Note: 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 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")
}
Try it Yourself »

Go Exercises

Test Yourself With Exercises

Exercise:

Comments in Go are written with a special character, which one?

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

Start the Exercise