将参数传递给函数参数不起作用

huangapple go评论95阅读模式
英文:

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)

https://go.dev/play/p/e3QemrZxB-S

huangapple
  • 本文由 发表于 2022年3月31日 19:38:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/71691808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定