1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Preallocate slices and remove double type assertion

This commit is contained in:
Tim Scheuermann 2019-10-11 23:13:18 +02:00
parent 25234e03da
commit ca18a14d43

View File

@ -8,7 +8,7 @@ import (
// MapToStrs takes a map of interfaces and returns a map of strings // MapToStrs takes a map of interfaces and returns a map of strings
func MapToStrs(aMap map[string]interface{}) map[string]string { func MapToStrs(aMap map[string]interface{}) map[string]string {
results := make(map[string]string) results := make(map[string]string, len(aMap))
for key, val := range aMap { for key, val := range aMap {
results[key] = val.(string) results[key] = val.(string)
@ -21,10 +21,10 @@ func MapToStrs(aMap map[string]interface{}) map[string]string {
// ToInts takes a slice of interfaces and returns a slice of ints // ToInts takes a slice of interfaces and returns a slice of ints
func ToInts(slice []interface{}) []int { func ToInts(slice []interface{}) []int {
results := []int{} results := make([]int, len(slice))
for _, val := range slice { for i, val := range slice {
results = append(results, val.(int)) results[i] = val.(int)
} }
return results return results
@ -32,14 +32,14 @@ func ToInts(slice []interface{}) []int {
// ToStrs takes a slice of interfaces and returns a slice of strings // ToStrs takes a slice of interfaces and returns a slice of strings
func ToStrs(slice []interface{}) []string { func ToStrs(slice []interface{}) []string {
results := []string{} results := make([]string, len(slice))
for _, val := range slice { for i, val := range slice {
switch val.(type) { switch t := val.(type) {
case int: case int:
results = append(results, strconv.Itoa(val.(int))) results[i] = strconv.Itoa(t)
case string: case string:
results = append(results, val.(string)) results[i] = t
} }
} }