reflect.MakeFunc在Go语言中的功能

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

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?

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))
}

答案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:

确定