Home >Backend Development >Golang >Why Does `reflect.MakeSlice` Return an Un-addressable Value in Go?

Why Does `reflect.MakeSlice` Return an Un-addressable Value in Go?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-07 21:56:03985browse

Why Does `reflect.MakeSlice` Return an Un-addressable Value in Go?

Why reflect.MakeSlice Returns Un-addressable Value in Go

Problem Overview

In Go, using reflect.MakeSlice to create a slice returns an un-addressable Value. This becomes an issue when a slice address is required, as demonstrated in the following code snippet:

collection.Find(bson.M{}).All(&result)

Obtaining a Pointer to a Slice Using Reflection

To resolve this issue, there are two solutions:

1. Using reflect.New()

The simplest solution is to use reflect.New() to create a pointer. For example:

my := &My{}

// Create a slice to begin with
myType := reflect.TypeOf(my)
slice := reflect.MakeSlice(reflect.SliceOf(myType), 10, 10)

// Create a pointer to a slice value and set it to the slice
x := reflect.New(slice.Type())
x.Elem().Set(slice)

collection.Find(bson.M{}).All(x.Interface())

2. Using a Typed Map

Alternatively, you can use a typed map to create a slice of the desired type:

type MyStructList map[string]*MyStruct

myMap := reflect.New(reflect.MapOf(myKey, myValue))

for _, value := range myMap.MapKeys() {
    myValue := myMap.MapIndex(value)
}

Why reflect.MakeSlice Returns Un-addressable Values

Local stack variables, including those created by reflect.MakeSlice, are not addressable in Go. This ensures that pointers to these variables remain meaningful and do not point to invalid memory locations.

Why a Pointer to a Slice is Required

The mgo API requires a pointer to a slice for its iter.All function because appending to a slice may reallocate memory. To communicate these changes to the caller, a pointer is necessary.

The above is the detailed content of Why Does `reflect.MakeSlice` Return an Un-addressable Value in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn