Tutorials References Exercises Videos Menu
Create Website Get Certified Upgrade

Go Comments


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

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

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")
}
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