GoLang

Explain Slice Composite Literal in Go Language | Go Programming Tutorial

Explain Slice Composite Literal in Go Language | Go Programming Tutorial

Explain Slice Composite Literal in Go Language | Go Programming Tutorial

In this article, you will learn about the Slice Composite Literals in the Go Programming Language.

In this two terminology is used i.e Slice and Composite Literals. A slice is a kind of data type which is similar to an array which is used to hold the items of the same data type. The only difference between array and slice data types is that slice can vary in size dynamically but it is not feasible in arrays. 
Composite Literals are used in constructing the values for other data types such as struct, slices, arrays, and maps. Each time they will be evaluated, and create a new value. They involve the type of literals which are followed by a brace-bound list of items. However, after this reading, you will get to know what is the meaning of the composite literal. 

Example:

package main
import "fmt"
  
func main() {
  
    // Slice allows you to group together
    // the values of the same type 
    // here type of values is int
    slice_ele := []int{30, 98, 70, 45}
      
    // displaying the values
    fmt.Println(slice_ele)
}

OUTPUT:

[30, 98, 70, 45]

Hope you understand the terminology composite literals. So, assigning the values to arrays, maps, and slices are done by using the composite literals. These are generally used to create a bunch of values of the same data type. 

Yamini  Sharma

I am working as a professional engineer in "HIghRadius Technologies Pvt. Ltd." Passionate about learning and exploring new technologies. Enthusiast in writing and sharing what I have in my knowledge bucket.