英文:
Passing parameter to a function parameter is not working
问题
我已经写好了以下代码。
package main
import (
"fmt"
"strings"
)
type student struct {
Name string
Age int
City string
}
func takeFuncAsParam(a func(st student, c string) bool, s []student) []student {
var result []student
for _, e := range s {
if a(e, c) {
result = append(result, e)
}
}
return result
}
func main() {
s1 := student{"Subir", 30, "Bolpur"}
s2 := student{"Mainak", 29, "Bolpur"}
s := []student{s1, s2}
filterByName := func(s student, c string) bool {
return strings.Contains(s.Name, c)
}
result := takeFuncAsParam(filterByName, s)
fmt.Println(result)
}
我在第17行遇到了编译错误。
undefined: c
那么在这种情况下,我该如何向filterByName函数传递参数(c string)?
英文:
I have written the following code.
package main
import (
"fmt"
"strings"
)
type student struct {
Name string
Age int
City string
}
func takeFuncAsParam(a func(st student, c string) bool, s []student) []student {
var result []student
for _, e := range s {
if a(e,c) {
result = append(result, e)
}
}
return result
}
func main() {
s1 := student{"Subir", 30, "Bolpur"}
s2 := student{"Mainak", 29, "Bolpur"}
s := []student{s1, s2}
filterByName := func(s student, c string) bool {
return strings.Contains(s.Name, c)
}
result := takeFuncAsParam(filterByName, s)
fmt.Println(result)
}
i am getting compilation error in line number 17.
undefined: c
So how can I pass a parameter to filterByName function in this case (c string)?
答案1
得分: 2
将takeFuncAsParam
的参数类型更改为func(student) bool
,即删除c string
参数,代码如下:
func takeFuncAsParam(fn func(student) bool, s []student) []student {
var result []student
for _, e := range s {
if fn(e) {
result = append(result, e)
}
}
return result
}
将filterByName
更改为一个函数,该函数接受要过滤的name
参数,并返回一个符合takeFuncAsParam
所需类型func(student) bool
的新函数,代码如下:
filterByName := func(name string) func(student) bool {
return func(s student) bool {
return strings.Contains(s.Name, name)
}
}
要使用新的过滤函数,您需要使用要过滤的name
参数调用它,然后将返回的函数传递给takeFuncAsParam
,代码如下:
fn := filterByName("Subir")
result := takeFuncAsParam(fn, s)
// 或者
result := takeFuncAsParam(filterByName("Subir"), s)
链接:https://go.dev/play/p/e3QemrZxB-S
英文:
Change the argument type of takeFuncAsParam
to be func(student) bool
, i.e. drop the c string
parameter, like so:
func takeFuncAsParam(fn func(student) bool, s []student) []student {
var result []student
for _, e := range s {
if fn(e) {
result = append(result, e)
}
}
return result
}
Change the filterByName
to be a function that takes the name
argument by which to filter and returns a new function of the type required by takeFuncAsParam
, i.e. func(student) bool
, like so:
filterByName := func(name string) func(student) bool {
return func(s student) bool {
return strings.Contains(s.Name, name)
}
}
To use the new filter function you need to call it with the name
argument by which to filter and then pass the returned function to takeFuncAsParam
, like so:
fn := filterByName("Subir")
result := takeFuncAsParam(fn, s)
// or
result := takeFuncAsParam(filterByName("Subir"), s)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论