To add an item to an existing array in golang, we have to make use of the built-in append
function. Here is an example:
main.go291 chars15 lines
Output:
main.go45 chars3 lines
In the above code snippet, we first create an array arr
of type int with 3 elements. We then use the append
function to add an element 4
at the end of the array. Note that the append
function returns a new slice, which we then assign to the original array arr
so that it now contains the new element.
gistlibby LogSnag