OneBite.Dev - Coding blog in a bite size

extract a sub-array from an array in Go

Code snippet on how to extract a sub-array from an array in Go

  mySlice := myArray[2:4]

This code is written in Go and it is used to extract a sub-array from an existing array. The syntax is quite straightforward: the sub-array is given by the range of indices, from 2 to 4 (exclusive of 4) within the existing array, denoted by ‘myArray’. The result of the extraction process is stored in the variable ‘mySlice’. It is important to note that the sub-array is not a copy of the original array, but a reference to the elements of it. This means that if the values of the original array are changed, the values in the sub array will also change.

go