Home >Backend Development >Golang >Structure slicing! =The interface slice it implements?
php editor Apple is here to reveal a puzzle about structure slicing: What is the difference between structure slicing and the interface slicing it implements? In the Go language, slices are a convenient and flexible data structure that can change size dynamically. Struct slicing is a special form of slicing, which stores elements of structure type. But how is it different from a slice that implements the same interface? Let us find out the answer together.
I have an interface Model
, which is implemented by struct Person
.
To get the model instance, I have the following helper function:
func newModel(c string) Model { switch c { case "person": return newPerson() } return nil } func newPerson() *Person { return &Person{} }
The above method allows me to return a Person instance of the correct type (new models can be easily added later using the same method).
When I try to do something like this to return a model slice, I get an error. Code:
func newModels(c string) []Model { switch c { case "person": return newPersons() } return nil } func newPersons() *[]Person { var models []Person return &models }
Go complains: Cannot use newPersons() (type []Person) as a return parameter of type []Model
My goal is to return a slice of whatever model type is requested (whether []Person
, []FutureModel
, []Terminator2000
, w/ e). What am I missing and how to properly implement such a solution?
This is very similar to the question I just answered: https://www.php.cn/link/2c029952e202c0e560626a4c5980d64c
The short answer is you are right. A slice of a struct is not equal to a slice of the interface implemented by the struct.
[]Person
and []Model
have different memory layouts. This is because the types they belong to have different memory layouts. Model
is an interface value, which means that in memory it is two words in size. One word represents type information and the other word represents data. Person
is a structure whose size depends on the fields it contains. In order to convert from []Person
to []Model
, you need to loop through the array and typecast each element.
Since this conversion is an O(n) operation and results in the creation of a new slice, Go refuses to perform this operation implicitly. You can do this explicitly using the following code.
models := make([]Model, len(persons)) for i, v := range persons { models[i] = Model(v) } return models
As dskinner pointed out , you most likely need a pointer to a slice, not a pointer to a slice. Pointers to slices are generally not needed.
*[]Person // pointer to slice []*Person // slice of pointers
The above is the detailed content of Structure slicing! =The interface slice it implements?. For more information, please follow other related articles on the PHP Chinese website!