I was trying to create a slice of maps the following way.
keyvalue := make(map[string]interface{})
keyvalueslice := make([]keyvalue, 1, 1)
I was trying to create it just like the way string slice is created, however I am getting an error saying keyvalue is not a type.
I am creating this slice to append data to keyvalueslice
variable later.
Can someone explain what is wrong?
In Go language, a slice can be created and initialized using the following ways: Using slice literal: You can create a slice using the slice literal. The creation of slice literal is just like an array literal, but with one difference you are not allowed to specify the size of the slice in the square braces[].
No, sorry, slices cannot be used as map keys.
Slices in Go and Golang The basic difference between a slice and an array is that a slice is a reference to a contiguous segment of an array. Unlike an array, which is a value-type, slice is a reference type. A slice can be a complete array or a part of an array, indicated by the start and end index.
Slices are similar to arrays, but are more powerful and flexible. Like arrays, slices are also used to store multiple values of the same type in a single variable. However, unlike arrays, the length of a slice can grow and shrink as you see fit.
keyvalue
is a variable not a type, you can't create a slice of variables. If you want to define custom type you can do this like
type keyvalue map[string]interface{}
then you can create a slice of keyvalue
s:
keyvalueslice := make([]keyvalue, 1, 1)
Example on playground
Or you can do this without defining custom type:
keyvalueslice := make([]map[string]interface{}, 1, 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With