Tips for Using Array and Slices in Golang

In this class, we’ll discuss how to use arrays and slices in Golang. By the end of the tutorial, you’ll be in a position to declare and use arrays and slices in Golang.
Let’s start by looking at the differences between arrays and slices in Golang.
An array can be defined as a data structure that’s used to store a fixed-size sequential collection of data elements that are of the same data type. Think of arrays as collections of variables that are of the same data type. For instance, if you have variables num1, num2, num3, num4 and num5 that are all integers (int32), we can declare these variables as one array instead of declaring five different variables.
On the other hand, slices are can be defined as abstractions that can take place over the Golang’s arrays. Golang’s arrays help you to define a variable that can hold multiple data items of the same type. However, it doesn’t provide you with in-built methods that can promote interaction with the array. This is a limitation that slices provide—they are many utility methods that slices provide to help you interact with arrays.
So, how can you define and use arrays and slices in Golang? Let’s dive in and learn tips for effective programming.
Golang’s Arrays
Here’s how you can declare arrays in Golang:

var name_of_array [SIZE] data type

The size of the array should be an integer constant that’s greater than 0. Here’s an example of how you can declare an array named balance that has 10 data elements of type float64:

var balance [10] float64

Here’s how you can initialize an array by using a single statement:

var balance = [10]float64{1000.0, 300.73, 33003.40, 7730.0, 5004.0,57603.0,57602.5,56570.0,6806.0,4049.7}

Array elements are accessed by indexing their names—where the index of the data element is placed within square brackets after the array name. Here’s an example:

float64 mybalance= balance [6]

Here’s an example of Golang code that uses arrays:

var balance [5] int32 
   	var a, b, c int32

   for a = 0; a < 5; i++ {
      balance [1] = i + 100   }
      fmt.Printf ("balance[a])

What about Go Slices?
We declare the Golang slices as an array that’s specified without specifying its size. Alternatively, you can use the “make” function to create one. Here’s an example:

var balance [] float64
balance = make ([] float64, 5, 5) 

If you declare your slice with no inputs, then it will be initialized as “nil”. This means that its length and capacity will be set to zero.
There two commonly used functions with Golang’s slices are “len ( )” and “cap ()” functions. The len () function returns the number of elements in a slice while the cap () function returns the capacity of a slice. Here’s an example:

func main ()
var balance = make ([] float64, 5, 5) 
myslice (balance)
func myslice (a [] int32){
   fmt.Printf ("The length and the capacity of slice are: ", len (a), cap (a))

The “append ( )” and “copy () functions allows you to increase the capacity of your slice. We use the copy () function to copy the contents of a source slice into destination slice while append () function adds data elements at the end of a Golang slice. Here’s an example:

package main
import "fmt"
func main () {
var balance [] int32
myslice (balance)
balance = append (balance, 0)
myslice (balance
balance = append (balance, 1)
myslice (balance)
balance = append (balance, 2, 3, 4)
myslice (balance)
balance1:= make ([] int32, len (balance), (cap (balance))*2)
copy (balance1, balance)
func myslice(a []int32){
fmt.Printf ("The length and the capacity of slice are: ", len (a), cap (a))

There you have it—keep on practicing to perfect the use of arrays and slices in Golang.
Online Technology Courses Stone River eLearning