A loop allows you to repeat a block of code multiple times until a specific condition is met.
for i := 0; i < 5; i++ { fmt.Print(i) }
A definite loop repeats a set number of times. It includes: - An initial statement to start with a variable, - A condition to check if the loop should continue, - A post statement to update the variable after each loop.
i := 0 // Start with 0 for i < 5 { fmt.Println(i) i++ // Increase the value by 1 each time }
An indefinite loop continues as long as a condition is true, without a fixed number of repetitions.
for { // This loop runs forever unless stopped manually // Your logic goes here } // This line will not be reached
In Go, you use only the 'for' keyword for all types of loops, whether you know the number of repetitions or not.
animals := []string{"Cat", "Dog", "Fish", "Turtle"} for i := 0; i < len(animals); i++ { if animals[i] == "Dog" { fmt.Println("Found the perfect animal!") break // Exit the loop } }
An infinite loop is a special type of indefinite loop that never ends because its condition is always true.
colors := []string{"green", "blue", "yellow", "red", "green", "yellow", "red"} for i := 0; i < len(colors); i++ { if colors[i] == "green" { continue // Skip this iteration } fmt.Println("You ate the", colors[i], "jellybean!") }
The 'break' keyword is used to exit a loop immediately.
letters := []string{"A", "B", "C", "D"} for i, letter := range letters { fmt.Println("Index:", i, "Letter:", letter) if letter == "C" { break // Exit the loop } }
The 'continue' keyword skips the rest of the current loop iteration and proceeds to the next one.
numbers := []int{1, 2, 3, 4, 5} for _, num := range numbers { if num%2 == 0 { continue // Skip even numbers } fmt.Println(num) // Print odd numbers only }
In Go, the 'range' keyword helps you loop through items in arrays or maps one at a time.
items := []string{"apple", "banana", "cherry"} for i, item := range items { fmt.Println("Index:", i, "Item:", item) }
In Go, an array is a fixed-size collection of elements of the same type. Arrays are useful for storing and managing related data. Example uses include:
- Holding sensor data - Calculating averages - Maintaining lists of items
You can create an empty array by specifying its size and type.
var myArray [3]int // Creates an array with 3 integer elements
You can initialize an array with values when you create it.
animals := [4]string{"Dog", "Hippo", "Cat", "Hamster"} // Array with 4 elements
To access elements in an array, use the index inside square brackets [].
animals := [4]string{"Dog", "Hippo", "Cat", "Hamster"} fmt.Println(animals[2]) // Prints "Cat"
You can change an element's value in an array by specifying its index and using the assignment operator =.
animals := [4]string{"Dog", "Hippo", "Cat", "Hamster"} animals[1] = "Lion" // Changes "Hippo" to "Lion"
Use the 'len' function to find out the length of an array.
animals := [4]string{"Dog", "Hippo", "Cat", "Hamster"} fmt.Println(len(animals)) // Prints 4
Arrays have a fixed size, while slices can grow or shrink. Slices are more flexible for handling collections of data.
var mySlice []int // Slice with no initial size
The capacity of an array is its size, which does not change. Slices have both length and capacity, which can be different.
var mySlice []int fmt.Println(cap(mySlice)) // Prints the capacity of the slice
You can create a slice either empty or with initial values.
mySlice := []int{1, 2, 3, 4} // Slice with initial values
You can get the length of an array or slice using the 'len' function.
names := []string{"Kathryn", "Martin", "Sasha", "Steven"} fmt.Println(len(names)) // Prints 4
You can modify elements in an array or slice by specifying the index and using the assignment operator.
names := []string{"Kathryn", "Martin", "Sasha", "Steven"} names[0] = "Alex" // Changes "Kathryn" to "Alex"
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!