【发布时间】:2014-02-06 04:56:15
【问题描述】:
问题发生在 Go 代码的第 17 行。下面是 python 和 Go 中的程序,所以你可以确切地看到我正在尝试做的事情。 Python 工作,我的 Go 尝试都失败了。已经背靠背阅读了 golang.org,而 google 也没有发现任何东西。
def my_filter(x):
if x % 5 == 0:
return True
return False
#Function which returns a list of those numbers which satisfy the filter
def my_finc(Z, my_filter):
a = []
for x in Z:
if my_filter(x) == True:
a.append(x)
return a
print(my_finc([10, 4, 5, 17, 25, 57, 335], my_filter))
现在,我遇到问题的 Go 版本:
package main
import "fmt"
func Filter(a []int) bool {
var z bool
for i := 0; i < len(a); i++ {
if a[i]%5 == 0 {
z = true
} else {
z = false
}
}
return z
}
func finc(b []int, Filter) []int {
var c []int
for i := 0; i < len(c); i++ {
if Filter(b) == true {
c = append(c, b[i])
}
}
return c
}
func main() {
fmt.Println(finc([]int{1, 10, 2, 5, 36, 25, 123}, Filter))
}
【问题讨论】:
标签: function parameters go nested arguments