Go Cheat Sheet


  // Comments in Go
  
  // In Go, comments are an essential part of making code readable and maintainable. There are two types of comments:
  
  // 1. Single-line comments
  // 2. Multi-line comments
  
  // Single-line comments
  // These comments start with two forward slashes (//) and extend to the end of the line.
  package main
  
  import "fmt"
  
  func main() {
      // This is a single-line comment
      fmt.Println("Hello, World!")  // Output: Hello, World!
  }
  
  // Multi-line comments
  /* 
  These comments start with a forward slash and an asterisk 
  and end with an asterisk and a forward slash.
  They can span multiple lines.
  */
  package main
  
  import "fmt"
  
  func main() {
      /*
      This is a multi-line comment.
      It can span multiple lines.
      */
      fmt.Println("Hello, World!")  // Output: Hello, World!
  }
  
  /*
  Important uses of comments in Go:
  
  1. Documentation: Comments are used to describe what a piece of code does, making it easier for others to understand.
  
  2. Disabling Code: During debugging, comments can temporarily disable code without deleting it.
  
  3. Documentation Comments: In Go, comments immediately preceding a package, function, type, or variable declaration are used to generate documentation.
  
  Example:
  */
  
  // Package greetings provides functions to greet users.
  package greetings
  
  import "fmt"
  
  // Hello returns a greeting for the named person.
  func Hello(name string) string {
      // Return a greeting that embeds the name in a message.
      message := fmt.Sprintf("Hi, %v. Welcome!", name)
      return message
  }
  
© 2024 CheatsheetCoder, All Rights Reserved.