Slices

Array

Array : collection of the same type with continuous memory.

Go’s arrays are values (value type) means whenever you assign an array to a new variable then the copy of the original array is assigned to the new variable.

This is also called as deep copy

The main issue an array has is that it can not be resized.

Arrays do not need to be initialized explicitly; the zero value of an array is a ready-to-use array whose elements are themselves zeroed

To overcome this problem we have Slices in golang.

Array example:


Slices 

Slices are the wrapper over arrays.

Slices do not own any data of their own, they are just a reference to the existing array.

Slices have length and capacity. Length is the number of elements present in the slice,

Capacity is the number of elements present in the underlying array.

New elements can be added to the slice using append function.

// Slices

var s []byte

// func make([]T, len, cap) []T
// where T stands for the element type of the slice to be created.
// The make function takes a type, a length, and an optional capacity.
// make allocates an array and returns a slice that refers to that array.
s = make([]byte, 5, 5)

fmt.Println("s:", s) // s: [0 0 0 0 0]
fmt.Println("len(s):", len(s)) // len(s): 5
fmt.Println("cap(s):", cap(s)) // cap(s): 5

s = append(s, 6)
fmt.Println("s:", s) // s: [0 0 0 0 0 6]
fmt.Println("len(s):", len(s)) // len(s): 6
fmt.Println("cap(s):", cap(s)) // cap(s): 16

// nil Vs empty slice

// The zero value of a slice is nil
// The len and cap functions will both return 0 for a nil slice.

// nil slice : useful when you want to represent a slice that doesn't exist,
// such as when expection occurs in a func that returns slice

var days []int // declared a varible but not initialize

// empty slice : useful when you want to represent an empty collection,
// such as when a database query returns zero results.

// 1. use a slice literal to create an empty slice of integers.
day := []int{} // declared and initialized it, set it as an empty slice

// 2. use make to create an empty slice of intergers .
slice := make([]int, 0)

fmt.Println("slice with make", slice) // slice with make []

fmt.Println("days", days) // days []
fmt.Println("len(days):", len(days)) // len(days): 0
fmt.Println("cap(days):", cap(days)) // cap(days): 0

if days == nil {
fmt.Println("zero value of slice is nil") // zero value of slice is nil
}

if day == nil { // will not execute its not nil , empty slice
fmt.Println("zero value of slice is nil")

}

Memory Allocation of slices

When the slice length is equal to its capacity and you try to append a new element to it,

then the new memory is allocated to the slice which has the double capacity as compared to the earlier capacity.

Slices are an important data structure in Go and are used extensively in many programs.

They are a flexible and efficient way to work with arrays and can save you time and

effort when coding in Go.

Append

In Go, the append function is used to add one or more elements to the end of a slice.
It can be used to increase the size of a slice or to append new elements to an existing slice.
Here's an example of how to use the append function to add an element to a slice:


The append function returns a new slice that includes the appended elements.
The original slice is not modified.

It's important to note that the append function has some limitations. It can only be
used with slices, and it can only append elements to the end of a slice. If you need to
insert elements at a specific position in a slice or delete elements from a slice, you'll
need to use other techniques.

Overall, the append function is a useful tool for adding elements to a slice and is used
frequently in Go programs.

Comments

Popular posts from this blog

Golang Interview Questions Part 1 Theory Top 50 Question

Complete Golang Development setup on Linux (Ubuntu 20.04 )

Best GitHub repositories for Go