There are two terms i.e. Slice and Composite Literal. Slice is a composite data type similar to an array which is used to hold the elements of the same data type. The main difference between array and slice is that slice can vary in size dynamically but not an array.
Composite literals are used to construct the values for arrays, structs, slices, and maps. Each time they are evaluated, it will create new value. They consist of the type of the literal followed by a brace-bound list of elements. (Did you get this point!) Well, after this read you will get to know what is a composite literal and you will be shocked that you know it already !!!!
Let’s see how to create a slice and make the use of composite literal:
// Go program to show the slice // - composite literal package main import "fmt" func main() { // Slice with composite literal // Slice allows you to group together // the values of the same type // here type of values is int s1 := []int{23, 56, 89, 34} // displaying the values fmt.Println(s1) } |
Output:
[23 56 89 34]
Hope you understand the term what exactly a composite literal. So basically assigning the values or initialization of arrays, slice, etc. are done using composite literals. These are generally used to compose a bunch of values of similar types.
Recommended Posts:
- How to copy one slice into another slice in Golang?
- How to create a Struct Instance Using a Struct Literal in Golang?
- How to convert a slice of bytes in uppercase in Golang?
- How to convert a slice of bytes in lowercase in Golang?
- Check if the given slice is sorted in Golang
- Searching an element of float64 type in Golang slice
- How to pass a Slice to Function in Golang?
- How to Sort a Slice of Strings in Golang?
- Check if the specified element is present in the slice of bytes in Golang
- How to count specific characters present in the slice in Golang?
- How to sort a slice of ints in Golang?
- How to sort a slice of float64s in Golang?
- Checking Slice of bytes for equality under Unicode case folding in Golang
- Check if the Slice of bytes starts with specified prefix in Golang
- Check if the Slice of bytes ends with specified suffix in Golang
- How to join the elements of the byte slice in Golang?
- How to replace a specified element in slice of bytes in Golang?
- How to replace all the elements in slice of bytes in Golang?
- How to repeat a slice of bytes in Golang?
- How to convert a slice of bytes in title case in Golang?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


