Comments in Golang are used to add extra information to your code such that it becomes more readable.

How Compiler Treats Comments in Golang

When the Go compiler encounters a comment then it simply ignores it. Comment is mostly used to add extra information such that your codebase becomes more readable

Types of Comments in Golang

The Go programming language provides two different ways to write comment

  • Single Line comment
  • Multi-Line comment

Single Line Comment

When the amount of information is less, a single-line comment is used. The single-line comment is written by prefixing your comment text with two forward slashes.

package main
import ("fmt")

func main() {
  // The next line print the Hello World text
  fmt.Println("Hello World!")
}

You can see that line number 5 is the single-line comment

Multi-Line Comment

You should use the multi-line comment when there is more information. Multi-line comments start with /* and ends with */.

package main
import ("fmt")

func add(a int, b int) {
  /* 
    The add function accepts two parameter
    first-parameter is integer
    second-paramter is integer
    sum of both the parameter is returned from the function
  */
  return a + b
}

Line Numbers 5 to 10 are the multiple-line comment

References

Leave a Reply

Your email address will not be published. Required fields are marked *