
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Initialize a Slice
In this article, we will learn how to initialize a slice using a variety of ways in examples. A slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas a slice is a dynamic array, meaning its value is not fixed and can be changed. Slices are more efficient and faster than arrays moreover they are passed by reference instead by value. Let's learn through examples how it can be executed.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 ? Create a function main and in that function initialize a slice using make function with two parameters length and capacity inside it.
Step 3 ? Print the slice initialized using make function on the console.
Step 4 ? The print statement is executed using fmt.Println() function where ln refers to new line.
Syntax
func make ([] type, size, capacity)
The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments
func append(slice, element_1, element_2?, element_N) []T
The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.
Using Make Function
In this example, we will learn how to initialize a slice with the help of make function. It is a type of built-in function whose working is described below. Let's understand how it works to solve this problem via the algorithm and code.
Example
package main import "fmt" func main() { slice := make([]int, 2, 4) // create slice using make function fmt.Println("The slice created here has a length of:", slice) //print slice }
Output
The slice created here has a length of: [0 0]
Using Shorthand Declaration
In this example, we will learn how to initialize a slice using shorthand declaration. The slice created will be printed on the console using print statement in Golang. Let's understand the concept via algorithm and the code.
Example
package main import "fmt" func main() { // creates a slice with elements slice := []int{1, 2, 3} fmt.Println("The slice created here is:") fmt.Println(slice) // print the slice }
Output
The slice created here is: [1 2 3]
Using Append Declaration
In this example we will initialize a slice using append function whose functioning is described below. Here, let's understand the concept with the help of algorithm and the code.
Example
package main import "fmt" func main() { var slice []int slice = append(slice, 1) //fill the elements in slice using append function slice = append(slice, 2) slice = append(slice, 3) fmt.Println("The slice created is:") fmt.Println(slice) // print the slice }
Output
The slice created is: [1 2 3]
Using a For Loop in Append Function
In this example, we will use an append function in for loop to initialize a slice. Using a loop helps us to assign elements to the slice. Let's understand it with the help of algorithm and the code.
Example
package main import "fmt" func main() { // create an empty slice slice := []int{} for i := 0; i < 4; i++ { slice = append(slice, i) // append the elements in slice using append function } fmt.Println("The slice created here is:") fmt.Println(slice) // print the slice }
Output
The slice created here is: [0 1 2 3]
Conclusion
In the above program, we used four examples to initialize a slice. In the first example we used make function to create a slice. In the second example we used shorthand declaration, in the third example we used append function to create a slice and in fourth example we used for loop to print the slice on the screen. Hence, program executed successfully.