How to initialize the slice with values using a slice literal?
A slice literal contain empty brackets followed by the type of elements the slice will hold, and a list of the initial values each element will have in curly braces.
Example
package main
import "fmt"
func main() {
var intSlice = []int{10, 20, 30, 40}
var strSlice = []string{"India", "Canada", "Japan"}
fmt.Printf("intSlice \tLen: %v \tCap: %v\n", len(intSlice), cap(intSlice))
fmt.Printf("strSlice \tLen: %v \tCap: %v\n", len(strSlice), cap(strSlice))
}
Most Helpful This Week
Golang Program to Triangle of Alphabets
How to set timeout for http.Get() requests in Golang?
How to get first and last element of slice in Golang?
Modernizing Legacy Applications: Critical Tips for Organizational Upgrades
Golang writing struct to JSON file
How to check specific field exist in struct?
How to concatenate two or more slices in Golang?
Illustration of Cigarette Smokers Problem in Golang
GO Program to Find LCM and GCD of given two numbers
Golang program for implementation of Radix Sort