如何动态反射(reflect)创建新的参数并调用函数?

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

how to dynamic reflect.New params and call the func?

问题

我想在接收HTTP调用时使用普通函数而不是HttpHandler,所以我应该动态地使用新参数调用该函数。

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. func main() {
  7. invoke(test)
  8. }
  9. func invoke(function interface{}) {
  10. funcType := reflect.TypeOf(function)
  11. if funcType.Kind() == reflect.Func {
  12. paramType := funcType.In(0).Elem()
  13. input := reflect.New(paramType)
  14. input.Elem().Field(0).SetString("neason")
  15. input.Elem().Field(1).SetInt(18)
  16. fmt.Println(input)
  17. funcValue := reflect.ValueOf(function)
  18. params := []reflect.Value{
  19. input.Elem(),
  20. }
  21. funcValue.Call(params)
  22. }
  23. }
  24. type Input struct {
  25. Name string
  26. Age int
  27. }
  28. type Output struct {
  29. Name string
  30. Age int
  31. }
  32. func test(input *Input) Output {
  33. return Output{
  34. Name: input.Name,
  35. Age: input.Age,
  36. }
  37. }

它会出现 panic: reflect: Call using *reflect.Value as type *main.Input 的错误。如何动态地使用 reflect.New 创建参数并调用该函数呢?

英文:

i want to use a normal func instead of a HttpHandler when receiving a http call
so i should dynamic new params to call the func.

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. func main() {
  7. invoke(test)
  8. }
  9. func invoke(function interface{}) {
  10. funcType := reflect.TypeOf(function)
  11. if funcType.Kind() == reflect.Func {
  12. paramType := funcType.In(0).Elem()
  13. input := reflect.New(paramType)
  14. input.Elem().Field(0).SetString("neason")
  15. input.Elem().Field(1).SetInt(18)
  16. fmt.Println(input)
  17. funcValue := reflect.ValueOf(function)
  18. params := []reflect.Value{
  19. reflect.ValueOf(input),
  20. }
  21. funcValue.Call(params)
  22. }
  23. }
  24. type Input struct {
  25. Name string
  26. Age int
  27. }
  28. type Output struct {
  29. Name string
  30. Age int
  31. }
  32. func test(input *Input) Output {
  33. return Output{
  34. Name: input.Name,
  35. Age: input.Age,
  36. }
  37. }

it will panic: reflect: Call using *reflect.Value as type *main.Input
how to dynamic reflect.New params and call the func ?

答案1

得分: 2

你有一个额外的reflect.Value包装层。直接使用input作为参数。它已经是一个reflect.Value

params := []reflect.Value{input}

https://go.dev/play/p/dpIspUFfbu0

英文:

You have an extra layer of reflect.Value wrappers. Use input directly as the parameter. It's already a reflect.Value.

  1. params := []reflect.Value{input}

https://go.dev/play/p/dpIspUFfbu0

huangapple
  • 本文由 发表于 2022年1月6日 12:33:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/70602510.html
匿名

发表评论

匿名网友

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

确定