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:
- break: Used to exit from a loop or switch statement prematurely.
- case: Used in switch statements to define individual cases.
- chan: Used for communication between concurrent goroutines.
- const: Used to declare constants.
- continue: Used to skip the rest of the loop and continue with the next iteration.
- default: Used in switch statements to define the default case.
- defer: Used to schedule a function call to be executed later (usually used for cleanup tasks).
- else: Used in if-else statements.
- fallthrough: Used in switch statements to fall through to the next case.
- for: Used to create loops.
- func: Used to define functions.
- go: Used to start a new goroutine (concurrent execution).
- goto: Used for unconditional jumps to a labeled statement.
- if: Used for conditional statements.
- import: Used to import packages.
- interface: Used to define interfaces, which are sets of method signatures.
- map: Used to declare and manipulate maps (key-value data structures).
- package: Used to define the package in which the code belongs.
- range: Used in for loops to iterate over elements in arrays, slices, maps, or strings.
- return: Used to exit a function and return a value.
- select: Used to perform non-blocking communication with multiple channels.
- struct: Used to define user-defined types (structs) with multiple fields.
- switch: Used to create multi-way conditionals.
- type: Used to define new types or type aliases.
- 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)
}
}