🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, we will learn how to work with Array in Golang with examples.
In Go, an array is a numbered sequence of elements of a specific length.
In other words, an array is a collection of elements of a single data type. An array holds a fixed number of elements, and it cannot grow or shrink.
Go - declare and initialize an array
The following example shows how to initialize an array in Go.
package main
import "fmt"
func main() {
var vals [2]int
fmt.Println(vals)
vals[0] = 1
vals[1] = 2
fmt.Println(vals)
}
Output:
[0 0] [1 2]
Use the below syntax to declare and initialize an array in one line:
package main
import "fmt"
func main() {
b := [5]int{1, 2, 3, 4, 5}
fmt.Println("dcl:", b)
}
Output:
[1 2 3 4 5] [1 2 3 0 0]
Go - array literal
Golang has array literals; we can specify the elements of the array between {} brackets.
In the example, we define two arrays with array literals.
package main
import "fmt"
func main() {
vals := [5]int{1, 2, 3, 4, 5}
fmt.Println(vals)
vals2 := [5]int{1, 2, 3}
fmt.Println(vals2)
}
Output:
[1 2 3 4 5] [1 2 3 0 0]
Go - array length
The length of the array is determined with the len() function.
In the example, we define an array of strings. We print the number of programming languages in the array.
package main
import "fmt"
func main() {
progLangs := [5]string{ "C", "C++", "Java", "Python", "Go" }
fmt.Println("There are", len(progLangs ), "programming languages in the array")
}
Output:
[1 2 3 4 5] [1 2 3 0 0]
Go - array indexing
Arrays are accessed with their index.
package main
import "fmt"
func main() {
var progLangs[5]string
progLangs[0] = "C"
progLangs[1] = "C++"
progLangs[2] = "Java"
progLangs[3] = "Python"
progLangs[4] = "Go"
fmt.Println(progLangs[0], progLangs[1])
fmt.Println(progLangs)
}
Output:
C C++ [C C++ Java Python Go]
Go - Array Iteration Example
With for loops, we can iterate over array elements in Go.
The example uses three for loop forms to iterate over an array of programming languages.
package main
import "fmt"
func main() {
progLangs := []string{ "C", "C++", "Java", "Python", "Go" }
for i := 0; i < len(progLangs); i++ {
fmt.Println(progLangs[i])
}
for idx, e := range progLangs {
fmt.Println(idx, "=>", e)
}
j := 0
for range progLangs {
fmt.Println(progLangs[j])
j++
}
}
Output:
C C++ Java Python Go 0 => C 1 => C++ 2 => Java 3 => Python 4 => Go C C++ Java Python Go
Go - Multidimensional Array Example
We can create multidimensional arrays in Go. We need additional pairs of square and curly brackets for additional array dimensions.
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
a := [2][2]int{
{1, 2},
{3, 4}, // the trailing comma is mandatory
}
fmt.Println(a)
var b [2][2]int
rand.Seed(time.Now().UnixNano())
for i := 0; i < 2; i++ {
for j := 0; j < 2; j++ {
b[i][j] = rand.Intn(10)
}
}
fmt.Println(b)
}
Output:
[[1 2] [3 4]] [[0 8] [7 2]]
Golang Related Tutorials
- Go (Golang) Functions with Examples
- Go (Golang) Operators with Examples
- Go ( Golang) - Read Input from User or Console
- Go (Golang) Read and Write File Example Tutorial
- Go (Golang) Array Tutorial
- Go (Golang) Slices Tutorial with Examples
- Go (Golang) Maps Tutorial with Examples
- Go (Golang) Structs Tutorial with Examples
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business

Comments
Post a Comment
Leave Comment