Go Comments #
A comment is text in your code that is completely ignored when the program runs.
👉 Comments help:
- Explain code
- Make code more readable
- Temporarily disable code during development or testing
✅ Single-line Comments #
Single-line comments start with //
.
Everything from //
to the end of the line is ignored by the compiler.
Example: #
// This is a comment
package main
import "fmt"
func main() {
// Print Hello World to the screen
fmt.Println("Hello World!")
}
You can also add a comment at the end of a line:
package main
import "fmt"
func main() {
fmt.Println("Hello World!") // This is a comment
}
✅ Multi-line Comments #
Multi-line comments start with /*
and end with */
.
All text between them is ignored.
Example: #
package main
import "fmt"
func main() {
/* The code below prints "Hello World" to the screen,
and it is a basic Go example */
fmt.Println("Hello World!")
}
💡 Tip: Use
//
for short comments and/* */
for longer explanations.
✅ Comment to Prevent Code Execution #
Comments are often used to temporarily disable code without deleting it.
Example: #
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
// fmt.Println("This line does not execute")
}
In this example, the second print statement is commented out and will not run.
🚀 Comments are essential for writing maintainable and clean code.