OneBite.Dev - Coding blog in a bite size

insert an element at the end of an array in Go

Code snippet on how to insert an element at the end of an array in Go

  arr := []int{1, 2, 3, 4}
  arr = append(arr, 5)

This code adds an element to the end of an array in Go. First, the array arr is declared with 4 integers, 1, 2, 3, and 4. Then the append function is used to take the array, arr, and add the integer 5 to the end of it. This modifies the original array, arr, which now holds the 5 integers, 1, 2, 3, 4, and 5. The array is then ready for use in the program.

go