create a slice with a length and capacity of type string in go

To create a slice with a specific length and capacity of type string in Go, we can use the built-in make() function. Here's an example code snippet:

main.go
package main

import (
	"fmt"
)

func main() {
	// Create a slice of strings with length and capacity of 5
	strSlice := make([]string, 5, 5)
	
	// Access the slice elements
	strSlice[0] = "hello"
	strSlice[1] = "world"
	strSlice[2] = "go"
	strSlice[3] = "slices"
	strSlice[4] = "example"
	
	// Print the slice
	fmt.Println(strSlice)
}
335 chars
21 lines

In this code, we create a slice of strings strSlice with length and capacity of 5 using the make() function. We then can access the individual elements of the slice and assign them values. Finally, we print the entire slice as output.

gistlibby LogSnag