Ad space (banner)
๐ŸนGo Lessons
Lesson 17 / 68

Writing Comments

In this lesson you will learn how to write comments in Go so the intent of your code is clear to other people and to your future self. This is for anyone searching for "Go comments syntax".

A comment is a note for humans that has no effect on execution. // starts a single-line comment and /* */ wraps a multi-line one.

The sample code shows all three patterns: a whole-line comment such as // This is a single-line comment, a trailing note as in price := 100 // product price, and a multi-line comment enclosed in /* */.

A common stumbling block for beginners is writing so many comments that the code gets harder to read. Recording why a piece of logic is needed is valuable; restating what the code already says is not.

In real-world development the godoc tool generates documentation straight from your comments. Comments are also the usual way to disable code temporarily, which makes them indispensable in practice.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Go directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)