Golang Tutorial
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays & Slices
String
Pointers
Interfaces
Concurrency
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.
switch
statement.switch
statements to define a default case.switch
statement to define a case.if
for alternative condition.switch
statements, making the program flow to move to the next case without a break.func sayHello() { fmt.Println("Hello!") }
m := map[string]int{ "apple": 5, "banana": 2, }
type Person struct { FirstName string LastName string Age int }
ch := make(chan int) go func() { ch <- 1 }() select { case v := <-ch: fmt.Println(v) default: fmt.Println("No value received.") }
nums := []int{1, 2, 3, 4, 5} if len(nums) > 3 { for _, v := range nums { fmt.Println(v) } } else { fmt.Println("Not enough numbers.") }
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.
Golang Reserved Words and Their Meanings:
package main import "fmt" func main() { // Reserved words var := 10 // Error: cannot use var as value fmt.Println(var) }
Keywords vs Identifiers in Golang:
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) }
Golang Built-in Keywords and Functions:
len
, make
, new
, etc.package main import "fmt" func main() { // Built-in function size := len("Golang") // Built-in keyword pointer := new(int) fmt.Println(size, pointer) }
Golang const
and var
Keywords Explained:
const
is used for constant declarations, while var
is used for variable declarations.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) }
Special Keywords in Golang (e.g., iota
):
iota
is a special keyword used in constant declarations to create incrementing values.package main import "fmt" const ( Monday = iota Tuesday Wednesday ) func main() { fmt.Println("Days:", Monday, Tuesday, Wednesday) }
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.package main import "fmt" func main() { // New keyword ptr := new(int) // Make keyword slice := make([]int, 5) fmt.Println(ptr, slice) }