首页 > 解决方案 > 如何在不执行变量的情况下将函数分配给变量?

问题描述

当我尝试将映射键分配给函数时,它会执行该函数。在不执行该功能的情况下如何做到这一点?

var myslice []int
myslice = append(myslice, 1)

func RemoveIndex(s []string, index int) []string {
    return append(s[:index], s[index+1:]...)
}

a["removeIndex"] = removeIndex(myslice, 0)

标签: go

解决方案


只需将功能分配给map[string]func([]string,int)[]string's 键

func RemoveIndex(s []string, index int) []string {
    return append(s[:index], s[index+1:]...)
}

a["removeIndex"] = RemoveIndex

rifn := a["removeIndex"]

rifn(myslice, 0)

推荐阅读