Golang Tutorial

Fundamentals

Control Statements

Functions & Methods

Structure

Arrays & Slices

String

Pointers

Interfaces

Concurrency

Keywords in Golang

In Go, keywords are reserved words that have a specific meaning and purpose within the language. You can't use these words as identifiers (e.g., variable or function names) in your programs. Let's explore these keywords with a brief description for each.

List of Go Keywords:

  • break: Used to terminate loops or a switch statement.
  • default: Used in switch statements to define a default case.
  • func: Used to declare a function.
  • interface: Used to declare an interface.
  • select: Used to choose from multiple send/receive channel operations.
  • case: Used in the switch statement to define a case.
  • defer: Schedules a function call to be executed after the current function completes.
  • go: Launches a function in a new goroutine.
  • map: Declares a map data type.
  • struct: Declares a composite data type consisting of fields.
  • chan: Declares a channel for communication between goroutines.
  • else: Used after if for alternative condition.
  • goto: Jumps to a labeled statement (not commonly used, as it's considered harmful in structured programming).
  • package: Defines a package, a way to group related Go code together.
  • switch: A multi-way branching statement.
  • const: Declares a constant value.
  • fallthrough: Used in switch statements, making the program flow to move to the next case without a break.
  • if: Conditionally executes a block of code.
  • range: Used in for-loops to iterate over items in data structures like arrays, slices, strings, or maps.
  • type: Defines a new type.
  • continue: Causes the current iteration of a loop to stop and begins the next iteration.
  • for: Used to declare loops.
  • import: Imports another package into the current package.
  • return: Exits a function, optionally returning a value.
  • var: Declares a variable.

Examples:

  • func:
func sayHello() {
    fmt.Println("Hello!")
}
  • map:
m := map[string]int{
    "apple":  5,
    "banana": 2,
}
  • struct:
type Person struct {
    FirstName string
    LastName  string
    Age      int
}
  • chan and select:
ch := make(chan int)

go func() {
    ch <- 1
}()

select {
case v := <-ch:
    fmt.Println(v)
default:
    fmt.Println("No value received.")
}
  • if, else, and range:
nums := []int{1, 2, 3, 4, 5}

if len(nums) > 3 {
    for _, v := range nums {
        fmt.Println(v)
    }
} else {
    fmt.Println("Not enough numbers.")
}

Conclusion

Understanding and properly using keywords is essential when programming in Go. This tutorial provided a quick overview of Go's keywords. As you progress and practice with the language, their usage will become second nature.

  1. Golang Reserved Words and Their Meanings:

    • Reserved words in Golang are keywords with predefined meanings and cannot be used as identifiers.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          // Reserved words
          var := 10 // Error: cannot use var as value
          fmt.Println(var)
      }
      
  2. Keywords vs Identifiers in Golang:

    • Keywords are reserved words with predefined meanings, while identifiers are user-defined names.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          // Identifier
          var myVariable int
      
          // Keyword (reserved word)
          var func int // Error: cannot use func as value
          fmt.Println(myVariable, func)
      }
      
  3. Golang Built-in Keywords and Functions:

    • Built-in keywords include functions like len, make, new, etc.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          // Built-in function
          size := len("Golang")
      
          // Built-in keyword
          pointer := new(int)
      
          fmt.Println(size, pointer)
      }
      
  4. Golang const and var Keywords Explained:

    • const is used for constant declarations, while var is used for variable declarations.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          // Const keyword
          const pi = 3.14
      
          // Var keyword
          var radius float64 = 5.0
      
          fmt.Println("Area:", pi*radius*radius)
      }
      
  5. Special Keywords in Golang (e.g., iota):

    • iota is a special keyword used in constant declarations to create incrementing values.
    • Example:
      package main
      
      import "fmt"
      
      const (
          Monday = iota
          Tuesday
          Wednesday
      )
      
      func main() {
          fmt.Println("Days:", Monday, Tuesday, Wednesday)
      }
      
  6. Golang new and make Keywords Difference:

    • new is used for dynamic memory allocation and returns a pointer, while make is used for creating slices, maps, and channels.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          // New keyword
          ptr := new(int)
      
          // Make keyword
          slice := make([]int, 5)
      
          fmt.Println(ptr, slice)
      }