reflect.MakeFunc在Go语言中的功能

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

Functioning of reflect.MakeFunc in Go-Lang

问题

我遇到了一个错误:undefined reflect.MakeFunc。为什么会这样?

package main

import (
"fmt"
"reflect"
)

func main() {
swap := func(in []reflect.Value) []reflect.Value {
return []reflect.Value{in[1], in[0]}
}
makeSwap := func(fptr interface{}) {
fn := reflect.ValueOf(fptr).Elem()
fn.Set(reflect.MakeFunc(fn.Type(), swap))
}
var intSwap func(int, int) (int, int)
makeSwap(&intSwap)
fmt.Println(intSwap(0, 1))
var floatSwap func(float64, float64) (float64, float64)
makeSwap(&floatSwap)
fmt.Println(floatSwap(2.72, 3.14))
}

英文:

I am getting an error: undefined reflect.MakeFunc.. Why so?

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. func main() {
  7. swap := func(in []reflect.Value) []reflect.Value {
  8. return []reflect.Value{in[1], in[0]}
  9. }
  10. makeSwap := func(fptr interface{}) {
  11. fn := reflect.ValueOf(fptr).Elem()
  12. fn.Set(reflect.MakeFunc(fn.Type(), swap))
  13. }
  14. var intSwap func(int, int) (int, int)
  15. makeSwap(&intSwap)
  16. fmt.Println(intSwap(0, 1))
  17. var floatSwap func(float64, float64) (float64, float64)
  18. makeSwap(&floatSwap)
  19. fmt.Println(floatSwap(2.72, 3.14))
  20. }

答案1

得分: 2

使用Go的发布版本:http://play.golang.org/p/Rw4uBFlAyc

英文:

Use the release version of Go: http://play.golang.org/p/Rw4uBFlAyc

huangapple
  • 本文由 发表于 2013年7月26日 21:05:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/17882089.html
匿名

发表评论

匿名网友

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

确定