func append(slice []Type, elems ...Type) []Type<\/code><\/pre>\n\n\n\nThe first argument is the slice itself. The second is the variable number of arguments which is<\/p>\n\n\n\n
elems ...Type<\/code><\/pre>\n\n\n\n\u2018\u2026\u2019<\/strong>\u00a0operator is the variadic syntax. So basically\u00a0\u2026Type<\/strong>\u00a0means that the append function can accept a variable number of arguments of type\u00a0Type<\/strong>. Below is the way for using this function. In the below code, we are appending 4 to a slice that has two elements. It appends at the end and returns the original slice. That is why we are collecting results again in\u00a0the numbers<\/strong>\u00a0variable. It is also ok to assign the result to some other variable.<\/p>\n\n\n\nnumbers := []int{1,2}\nnumbers := append(numbers, 4) \/\/Slice will become [1, 2, 3, 4]<\/code><\/pre>\n\n\n\nIt is also ok to append any number of elements because the second argument is the variadic argument.<\/p>\n\n\n\n
numbers := []int{1,2}\nnumbers := append(numbers, 3, 4, 5) \/\/Slice will become [1, 2, 3, 4, 5]<\/code><\/pre>\n\n\n\nThis function in the background increases the length and capacity of the slice. There are two cases<\/p>\n\n\n\n
- When slice length is less than capacity<\/li><\/ul>\n\n\n\n
- When slice length is equal to capacity<\/li><\/ul>\n\n\n\n
<\/span>When slice length is less than capacity<\/strong><\/span><\/h3>\n\n\n\nIn this case, by using the append function,\u00a0 the length of the slice will be increased by one without any change in its capacity. Let\u2019s see an example<\/p>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n numbers := make([]int, 3, 5)\n numbers[0] = 1\n numbers[1] = 2\n numbers[2] = 3\n fmt.Printf(\"numbers=%v\\n\", numbers)\n fmt.Printf(\"length=%d\\n\", len(numbers))\n fmt.Printf(\"capacity=%d\\n\", cap(numbers))\n\n \/\/Append number 4\n numbers = append(numbers, 4)\n fmt.Println(\"\\nAppend Number 4\")\n fmt.Printf(\"numbers=%v\\n\", numbers)\n fmt.Printf(\"length=%d\\n\", len(numbers))\n fmt.Printf(\"capacity=%d\\n\", cap(numbers))\n\n \/\/Append number 5\n numbers = append(numbers, 4)\n fmt.Println(\"\\nAppend Number 5\")\n fmt.Printf(\"numbers=%v\\n\", numbers)\n fmt.Printf(\"length=%d\\n\", len(numbers))\n fmt.Printf(\"capacity=%d\\n\", cap(numbers))\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nnumbers=[1 2 3]\nlength=3\ncapacity=5\n\nAppend Number 4\nnumbers=[1 2 3 4]\nlength=4\ncapacity=5\n\nAppend Number 5\nnumbers=[1 2 3 4 4]\nlength=5\ncapacity=5<\/code><\/pre>\n\n\n\nCapacity in all cases doesn\u2019t change and it is 5 while length increases by 1.<\/p>\n\n\n\n
<\/span>When slice length is equal to the capacity<\/strong><\/span><\/h3>\n\n\n\nIn this case, since there is no more capacity, so no new elements can be accommodated.\u00a0 So in this case under the hood, an array of double the capacity will be allocated. The current array pointed by the slice will be copied to that new array. Now the slice will start pointing to this new array. Hence the capacity will be doubled and length will be increased by 1. Let\u2019s see an example<\/p>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n numbers := make([]int, 3, 3)\n numbers[0] = 1\n numbers[1] = 2\n numbers[2] = 3\n\n fmt.Printf(\"numbers=%v\\n\", numbers)\n fmt.Printf(\"length=%d\\n\", len(numbers))\n fmt.Printf(\"capacity=%d\\n\", cap(numbers))\n\n \/\/Append number 4\n numbers = append(numbers, 4)\n fmt.Println(\"\\nAppend Number 4\")\n fmt.Printf(\"numbers=%v\\n\", numbers)\n fmt.Printf(\"length=%d\\n\", len(numbers))\n fmt.Printf(\"capacity=%d\\n\", cap(numbers))\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nnumbers=[1 2 3]\nlength=3\ncapacity=3\n\nAppend Number 4\nnumbers=[1 2 3 4]\nlength=4\ncapacity=6<\/code><\/pre>\n\n\n\nNotice in the above example that the capacity is doubled.<\/p>\n\n\n\n
It is also possible to append one slice to another slice. Below is the format for that.<\/p>\n\n\n\n
res := append(slice1, slice2...)<\/code><\/pre>\n\n\n\nNotice\u00a0\u2018\u2026\u2019<\/strong>\u00a0after the second slice.\u00a0\u2018\u2026\u2019\u00a0<\/strong>is the operator which means that the argument is a variadic parameter. Meaning that during run time slice2\u00a0will be expanded to its individual elements which are passed as multiple arguments to the append function.<\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n numbers1 := []int{1, 2}\n numbers2 := []int{3, 4}\n numbers := append(numbers1, numbers2...)\n fmt.Printf(\"numbers=%v\\n\", numbers)\n fmt.Printf(\"length=%d\\n\", len(numbers))\n fmt.Printf(\"capacity=%d\\n\", cap(numbers))\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nnumbers=[1 2 3 4]\nlength=4\ncapacity=4<\/code><\/pre>\n\n\n\n <\/p>\n\n\n\n