How to initialize a slice with values in Golang

#golang

Last updated on Sep 6, 2021 by Suraj Sharma



In this tutorial, you’ll learn how you can initialize a new slice with values in Golang using a composite literal.

Composite literals are used to initialize slices, maps, and structs with initial values between the curly braces.


Example:


import "fmt"

func main() {
    primeNumbers := []int{1, 2, 3, 5, 7, 11}

    fmt.Println(primeNumbers)
}


Related Solutions


Rate this post


Suraj Sharma is a Full Stack Software Engineer. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.