1

How can I instance an array with a given type?

I get the type this way:

type := reflect.ValueOf(obj)

But I don't know how to get an instance of an array with this type

1 Answer 1

3

Use reflect.MakeSlice for that:

type := reflect.ValueOf(obj)
reflectSlice := reflect.MakeSlice(type, sliceLength, sliceCapacity)
Sign up to request clarification or add additional context in comments.

1 Comment

In case he really meant array and not slice, then he'll have to wait for CL4111.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.