Go Keywords

In Go (Golang), keywords are reserved words with specific meanings and functionalities within the language. They are part of the Go syntax and serve as building blocks for creating programs. As keywords have predefined purposes in Go, you cannot use them as identifiers (variable names, function names, etc.).

Here is a list of all the keywords in Go:

break      default       func      interface      select
case       defer         go        map            struct
chan       else          goto      package        switch
const      fallthrough   if        range          type
continue   for           import    return         var

Let’s briefly explain each keyword:

  1. break: Used to exit from a loop or switch statement prematurely.
  2. case: Used in switch statements to define individual cases.
  3. chan: Used for communication between concurrent goroutines.
  4. const: Used to declare constants.
  5. continue: Used to skip the rest of the loop and continue with the next iteration.
  6. default: Used in switch statements to define the default case.
  7. defer: Used to schedule a function call to be executed later (usually used for cleanup tasks).
  8. else: Used in if-else statements.
  9. fallthrough: Used in switch statements to fall through to the next case.
  10. for: Used to create loops.
  11. func: Used to define functions.
  12. go: Used to start a new goroutine (concurrent execution).
  13. goto: Used for unconditional jumps to a labeled statement.
  14. if: Used for conditional statements.
  15. import: Used to import packages.
  16. interface: Used to define interfaces, which are sets of method signatures.
  17. map: Used to declare and manipulate maps (key-value data structures).
  18. package: Used to define the package in which the code belongs.
  19. range: Used in for loops to iterate over elements in arrays, slices, maps, or strings.
  20. return: Used to exit a function and return a value.
  21. select: Used to perform non-blocking communication with multiple channels.
  22. struct: Used to define user-defined types (structs) with multiple fields.
  23. switch: Used to create multi-way conditionals.
  24. type: Used to define new types or type aliases.
  25. var: Used to declare variables.

Remember that keywords are an integral part of Go’s syntax, and using them correctly is essential for creating valid and efficient Go programs. When naming identifiers (variables, functions, etc.), ensure that you do not use any of the keywords listed above. By following the rules and conventions associated with Go’s keywords and identifiers, you can write clean and well-structured Go code.

Some examples that demonstrate the use of some keywords in Go:

if-else Statement:

package main

import "fmt"

func main() {
    age := 25

    if age >= 18 {
        fmt.Println("You are an adult.")
    } else {
        fmt.Println("You are a minor.")
    }
}

switch Statement:

package main

import "fmt"

func main() {
    day := "Monday"

    switch day {
    case "Monday":
        fmt.Println("It's Monday.")
    case "Tuesday":
        fmt.Println("It's Tuesday.")
    default:
        fmt.Println("It's another day.")
    }
}

const Keyword:

package main

import "fmt"

const pi = 3.14159

func main() {
    fmt.Println("Value of Pi:", pi)
}

func Keyword:

package main

import "fmt"

func add(a, b int) int {
    return a + b
}

func main() {
    result := add(10, 20)
    fmt.Println("Result:", result)
}

range Keyword:

package main

import "fmt"

func main() {
    numbers := []int{1, 2, 3, 4, 5}

    for index, value := range numbers {
        fmt.Printf("Index: %d, Value: %d\n", index, value)
    }
}