Skip to content

reflect: add SliceAt function #61308

Closed
Closed
@ianlancetaylor

Description

@ianlancetaylor

In Go 1.21 we are introducing some new functions in the unsafe package: unsafe.Slice, unsafe.SliceData, unsafe.String, unsafe.StringData. It should be possible to access this functionality through the reflect package as well.

The value returned by unsafe.SliceData is already available by calling the reflect.Value.Pointer method.

I propose that we extend reflect.Value.Pointer so that if it is called on a value of kind String, it returns the equivalent of unsafe.StringData.

The equivalent to unsafe.String is simply reflect.ValueOf(unsafe.String((*byte)(vp.UnsafePointer()), n)), so I don't think we need to add anything for that.

I propose one new function:

package reflect

// UnsafeSlice returns a Value representing a slice whose underlying data starts at vp,
// with length and capacity equal to n. vp.Kind() must be Pointer.
// The returned slice will have element type vp.Type().Elem().
// This is like unsafe.Slice.
func UnsafeSlice(vp Value, n int) Value

(edited to change function name from Slice to UnsafeSlice`)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions