Golang Append Function
Overview
A slice is similar to an array with the difference that the size of the slice can be changed dynamically. The append() method of the slice can be used to append new elements at the end of the given slice. We can also append one slice to another using the … operator.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Introduction
The golang append method helps us add any number of items in the slice or at the end of the slice. This append function is built-in and adds an element at the end of the slice. The following are the conditions for using the append function:
- The underlying array is reused if there is enough capacity
- If there is not enough capacity, then a new underlying array is allocated along with copying over the data
The golang append method returns the updated slice and therefore the result needs to be stored in a variable that is holding the slice.
Syntax
The syntax of the golang append function is as follows:
Here, in this syntax, the function takes the parameter as an s slice, and x, T is a variable that takes a variable number of arguments for the following x parameter. These types of functions are called variadic functions. Parameters and return value The following parameters are accepted by the Append in Golang:
- A slice of Go language but a valid one
- Elements that are to be appended after a slice given should be of the same type as that of the slice.
Note: the function append() returns a slice that is updated as is of the same type as that of the input slice.
Append Example
Turn Learning into Career Growth
Behaviour of capacity of the slice
The slice can give five pieces of information when the data is appended:
- The length by using the method len(slice)
- The capacity by using the method cap(slice)
- The elements inside the slice by using fmt.Sprintf("%v", slice)
- The memory or the location where the values of the elements are stored by using fmt.Sprintf("%p", slice)
- The type of the elements stored in the slice by using fmt.Sprintf("%T", slice)
Let's look at the example and understand how to Append in Golang:
Output:
Note: the output can be different for different users since the address varies. Apart from this, everything else is the same.
Explanation: In this example, every time an item is added both the parameters, len(numbers), and items in the numbers slice change. Every time the len(numbers) and cap(numbers) are the same, the slice will be on its max capacity and if another item is added to the slice then the capacity of the slice grows. The address of the slice also changes when the maximum capacity is changed.
When the fifth number is added to the slice, you will notice that the pc or your computer has allocated a new piece of memory for it.
Append one Slice to Another
The code given below demonstrates how to use the golang append function to append one slice to another in Golang
Output:
Explanation: In this example, the first slice is created and printed after which an element is appended to the first slice and is printed. In example 2, however, two elements are appended in the same slice that is created at the start. In example 3, a slice is created and the entire slice is appended in the first slice as shown in the code.
Append the string to a byte slice
We can also append a string to a byte slice, let's look at the code snippet given below to learn how to append a string to a byte slice
Output:
Explanation: It is similar to that of normal append, instead in this case, we just had to unpack the string to byte. The resulting byte is displayed at the output.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Conclusion
- A slice is similar to an array with the difference that the size of the slice can be changed dynamically. The append() method of the slice can be used to append new elements at the end of the given slice. We can also append one slice to another using the… operator.
- The following are the conditions for using the append function:
- The underlying array is reused if there is enough capacity
- If there is not enough capacity, then a new underlying array is allocated along with copying over the data
- The append method returns the updated slice and therefore the result needs to be stored in a variable that is holding the slice.