Working with Arrays in Go(golang)

Arrays

var students = [3]string
var numbers = [3]int{27, 42, 11}
var animals = [...]string{"Lion", "Gorilla", "Cat", "Dog"}
students := [...]string{"Sean", "Elaina", "Clark"}
fmt.Println(len(students)) //method used to print to console
//returns 3 as we have 3 elements in the array

Nested Arrays

var twoDimendionalArray[3][3]int = [3][3]int{[3]int{1, 1, 0}, [3]int{0, 1, 1}, [3]int{0, 0, 1}}
/*
We initializing our array to contain 3 elements within the array and the arrays within that array are going to also contain 3 elements.
arrays are 0 indexed, meaning that they start from zero instead of starting at 1.
we are then assigning first element of the array to contain an array with 1, 1, 0 and following the same process with the second and third element in the array
*/
var twoDimendionalArray[3][3]int
twoDimendionalArray[0] = [3]int{1, 1, 0}
twoDimendionalArray[1] = [3]int{0, 1, 1}
twoDimendionalArray[2] = [3]int{0, 0, 1}
[
[1,1,0]
[0,1,1]
[0,0,1]
]

Array Pointer

array1 := int[3]{33,63,74}
array2 := firstArray
array1 := int[3]{33,63,74}
array2 := &array1
array2[0] = 69
fmt.Println(array1)
fmt.Println(array2)
//Output:
/*
[69,63,74]
[69,63,74]
It changes both of the arrays because array2 is not pointing to array1 instead of just making a copy of array1
*/

Resources

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Are Single-Page Applications Bounded Contexts — and who the heck cares?

Easiest way to create a new user with home directory and default settings on Ubuntu

PHP design patterns (Creational : part 1)

Spring IOC Container

MicroPython — OTA Updates and GitHub, a match made in heaven

How I’m using Cubits from the Bloc library to manage my states

Rock To Code: The Bookends of Disruption

💙 QUBE Launchpad х AssetFi 💙 AMA Session is coming tomorrow!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sean Dever

Sean Dever

More from Medium

Leetcode with Golang: Integer to Roman Conversion

The main idea of ​​implementing a binary tree with Golang

Golang — basic unit testing and benchmarking

Battle of Concurrency | Goroutines vs Threads.