Hone logo
Hone
Problems

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:

  1. length: An integer representing the desired number of elements in the slice.
  2. defaultValue: An interface{} value representing the value to pre-fill each element of the slice with.
  3. sliceType: A reflect.Type representing 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 (reflect package) to achieve type safety and dynamic slice creation.
  • The function should handle cases where length is zero or negative.
  • The defaultValue should be correctly assigned to each element, respecting the sliceType.

Expected Behavior:

  • If length is 0 or negative, an empty slice of the correct type should be returned.
  • If length is positive, a slice of that length should be created, and all its elements should be set to defaultValue.

Edge Cases to Consider:

  • length being 0.
  • length being negative.
  • defaultValue being nil and the sliceType expecting a non-nil value (though for basic types, this is usually handled by Go's zero value).
  • sliceType being 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 length parameter will be an int.
  • The defaultValue parameter will be an interface{}.
  • The sliceType parameter will be a reflect.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 reflect package.

Notes

  • The reflect package is powerful but can be complex. Focus on using reflect.New to create values and reflect.MakeSlice to create slices.
  • Remember that reflect.Value objects need to be manipulated carefully. You'll likely need to use methods like Set to assign values.
  • Consider how to create a reflect.Value from your defaultValue to assign it to the slice elements.
  • The zero value for a type can be obtained using reflect.Zero(sliceType).
Loading editor...
go