go - 如何在不执行变量的情况下将函数分配给变量?
问题描述
当我尝试将映射键分配给函数时,它会执行该函数。在不执行该功能的情况下如何做到这一点?
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)
解决方案
只需将功能分配给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)
推荐阅读
- windows - 如何批量获取未识别的网络适配器名称
- gpu - 关于 docker、nvidia-docker、cudnn 和 tensorflow 的问题
- rust - 如何为特征提供 IntoIterator 的默认实现?
- c# - 在 MEF 中的泛型方法中转换类
- html - 我的浏览器中仍然显示隐藏的列,这是什么问题?
- vue.js - 如何将垂直“b-tabs”导航部分和内容部分的高度设置为相同(基于内容部分)
- function - 在 Go 中是否可以使用命名参数调用函数?
- label - 使用python在Gtk中的标签内垂直居中文本
- wordpress - 我们能否重做一个在 wordpress.org 上发布的插件,并为已在该站点上安装该插件的用户禁用更新
- batch-file - 使用 JQ 在 Windows shell 中通过子元素的内容过滤 JSON 的元素?