A comprehensive guide to data structures in Go
Data structures play a fundamental role in computer science and programming by providing efficient ways to organize and manipulate data. In Go (or Golang), a statically typed language designed for simplicity and efficiency, data structures are essential for building robust and scalable applications.
Common data structures in Go include arrays, slices, maps, structs, and pointers. Arrays in Go are fix-size sequences of elements with a specific type, while slices are dynamic, resizable views of arrays. Maps are hash tables used for key-value pair storage, providing efficient lookup, insertion, and deletion operations. Structs allow developers to define custom composite data types by grouping together named fields of different types. Pointers in Go are variables that store memory addresses, enabling efficient memory management and indirect access to data.
Understanding data structures in Go is crucial for writing efficient and idiomatic code. Developers should be familiar with the characteristics, operations, and trade-offs of each data structure to choose the most suitable one for specific use cases. Mastery of data structures empowers developers to design elegant and performant solutions to complex problems in Go programming.A comprehensive guide to data structures in Go.
-
A structure is a collection of data fields with declare data types. Golang can declare and create its data types by combining one or more types, including both built-in and user-defined types.
package main
import "fmt"
type student struct {
Name string
class string
Roll_No int
Marks int
}
func main() {
var s1 = student{"Sandeep", "X", 2001, 98}
var s2 student
s2.Name = "Sonu"
s2.class = "IX"
s2.Roll_No = 2039
s2.Marks = 56
fmt.Println(s1)
fmt.Println(s2)
}
Output :
PS C:\GO_Language\Structure> go run structure.go
{Sandeep X 2001 98}
{Sonu IX 2039 56}
- Accessing Structure members
package main
import "fmt"
type Books struct {
title string
author string
subject string
book-id int
}
func main() {
var Book1 Books
var Book2 Books
Book1.title = "Go Programming"
Book1.author = "Kreston"
Book1.subject = "Go programming tutorial"
Book1.book-id = 398312389
Book2.title = "Python Programming"
Book2.author = "Hazera Ali"
Book2.subject = "Python Programming tutorial"
Book2.book-id = 392342334
fmt.Printf("Book 1 Title :%s\n", Book1.title)
fmt.Printf("Book 1 author :%s\n", Book1.author)
fmt.Printf("Book 1 subject :%s\n", Book1.subject)
fmt.Printf("Book 1 book id :%s\n", Book1.book-id)
fmt.Printf("Book 2 Title :%s\n", Book2.title)
fmt.Printf("Book 2 author :%s\n", Book2.author)
fmt.Printf("Book 2 subject :%s\n", Book2.subject)
fmt.Printf("Book 2 book id :", Book2.book-id)
}
Output :
PS C:\GO_Language\Structure> go run structure1.go
Book 1 Title :Go Programing
Book 1 author :Kriston
Book 1 subject :Go programming tutorial
Book 1 book id :%!s(int=398312389)
Book 2 Title :Python Programming
Book 2 author :HZara Ali
Book 2 subject :Python programming tutorial
Book 2 book id :%!(EXTRA int=392342334)
- Golang program to illustrate the pointer to struct
package main
import "fmt"
// defining a structure
type Employee struct {
fName, lName string
age, salary int
}
func main() {
// passing the address of struct variable
// emp is a pointer to the Employee struct
emp := &Employee{"Sandeep ", "Sharma", 23, 10000}
emp2 := &Employee{"GuruDatt", "Shet", 40, 60000}
// (*emp).firstName is the syntax to access
// the firstName field of the emp8 struct
fmt.Println("First Name:", (*emp).fName)
fmt.Println("Age:", (*emp).age)
fmt.Println("Last Name:", (*emp2).lName)
fmt.Println("Age:", (*emp2).age)
}
check Output :
PS C:\GO_Language\Structure> go run structure3.go
First Name: Sandeep
Age: 23
Last Name: Shet
Age: 40