-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: reflects - add new func TypeReal, TypeElem, MakeSliceByElem
- Loading branch information
Showing
6 changed files
with
72 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,20 @@ | ||
// Package reflects Provide extends reflect util functions. | ||
package reflects | ||
|
||
import "reflect" | ||
|
||
// MakeSliceByElem create a new slice by the element type. | ||
// | ||
// - elType: the type of the element. | ||
// - returns: the new slice. | ||
// | ||
// Usage: | ||
// | ||
// sl := MakeSliceByElem(reflect.TypeOf(1), 10, 20) | ||
// sl.Index(0).SetInt(10) | ||
// | ||
// // Or use reflect.AppendSlice() merge two slice | ||
// // Or use `for` with `reflect.Append()` add elements | ||
func MakeSliceByElem(elTyp reflect.Type, len, cap int) reflect.Value { | ||
return reflect.MakeSlice(reflect.SliceOf(elTyp), len, cap) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters