Go Slice Maker
This challenge focuses on implementing a fundamental Go programming concept: creating and manipulating slices. You will build a function that generates a slice of a specified type and size, pre-filled with a default value. This is a common operation in Go when you need to initialize data structures.
Problem Description
Your task is to create a Go function called MakeSlice that takes three arguments:
length: An integer representing the desired number of elements in the slice.defaultValue: An interface{} value representing the value to pre-fill each element of the slice with.sliceType: Areflect.Typerepresenting the underlying type of the elements in the slice.
The MakeSlice function should return a new slice of the specified sliceType with the given length, where each element is initialized to defaultValue.
Key Requirements:
- The function must use Go's reflection capabilities (
reflectpackage) to achieve type safety and dynamic slice creation. - The function should handle cases where
lengthis zero or negative. - The
defaultValueshould be correctly assigned to each element, respecting thesliceType.
Expected Behavior:
- If
lengthis 0 or negative, an empty slice of the correct type should be returned. - If
lengthis positive, a slice of that length should be created, and all its elements should be set todefaultValue.
Edge Cases to Consider:
lengthbeing 0.lengthbeing negative.defaultValuebeingniland thesliceTypeexpecting a non-nil value (though for basic types, this is usually handled by Go's zero value).sliceTypebeing an invalid or unsupported type (this challenge assumes valid types for simplicity, but in a real-world scenario, you'd add checks).
Examples
Example 1:
Input:
length: 5
defaultValue: 0
sliceType: reflect.TypeOf(0) // int
Output:
[]int{0, 0, 0, 0, 0}
Explanation:
A slice of integers of length 5 is created, with each element initialized to the integer value 0.
Example 2:
Input:
length: 3
defaultValue: "hello"
sliceType: reflect.TypeOf("") // string
Output:
[]string{"hello", "hello", "hello"}
Explanation:
A slice of strings of length 3 is created, with each element initialized to the string value "hello".
Example 3:
Input:
length: 0
defaultValue: 100
sliceType: reflect.TypeOf(100) // int
Output:
[]int{}
Explanation:
When length is 0, an empty slice of the specified type is returned.
Example 4:
Input:
length: -2
defaultValue: true
sliceType: reflect.TypeOf(true) // bool
Output:
[]bool{}
Explanation:
When length is negative, an empty slice of the specified type is returned.
Constraints
- The
lengthparameter will be anint. - The
defaultValueparameter will be aninterface{}. - The
sliceTypeparameter will be areflect.Type. - The function should aim for reasonable performance; avoid extremely inefficient reflection operations if possible, although the primary focus is correctness.
- You are allowed to use the standard Go
reflectpackage.
Notes
- The
reflectpackage is powerful but can be complex. Focus on usingreflect.Newto create values andreflect.MakeSliceto create slices. - Remember that
reflect.Valueobjects need to be manipulated carefully. You'll likely need to use methods likeSetto assign values. - Consider how to create a
reflect.Valuefrom yourdefaultValueto assign it to the slice elements. - The zero value for a type can be obtained using
reflect.Zero(sliceType).