To add an item to a slice of type int in Go, you can use the built-in append()
function which returns a new slice containing the original slice with the added item. The append()
function adds the new item to the end of the slice.
Here's an example of adding an item to a slice of type int:
main.go347 chars17 lines
In the second example, we first append {5}
to the start of a slice containing only the value {2}
, and then append the result of that to slice[2:]
using the 3 dot ...
syntax to indicate that each item of the slice is being passed as a separate parameter. This results in the {5}
integer being inserted between the {2}
and {3}
in the slice.
gistlibby LogSnag