We can’t always be there in person to explain to the next developer (or even our future self) what our code does or what our intentions were when writing it. That’s where comments come in.

Comments are ignored by the compiler and helpful for many things. We can use comments to explain our code, or thought process, and even to comment out code to debug (fix errors).

Go also encourages the use of comments for describing what functions do, it gets used by Go’s documentation tool. In fact, it started on their Github Wiki page!

There are two types of comments in Go. A line comment is created using //:

// This entire line is ignored by the compiler // fmt.Println("Does NOT print") fmt.Println("This gets printed!") // This part gets ignored

Notice that line comments out the content to the right of //.

There are also block comments that can span multiple lines— it starts with /*, ends with a */ and envelopes everything inside (including new lines):

/* This is ignored. This is also ignored. fmt.Println("This WON'T print!") */

In the example above we’ve commented out all three lines using a block comment.

Let’s use some comments in our own code!



Use a line comment, //, to comment out Are we racing or coding?


Use a block comment, /* */ to comment out the first TWO fmt.Println() statements.


Run the program using the command line and the command go run main.go. Press enter to see what is printed out!

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?