Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some questions from MakeFunc #26226

Closed
lanmingle opened this issue Jul 5, 2018 · 2 comments
Closed

Some questions from MakeFunc #26226

lanmingle opened this issue Jul 5, 2018 · 2 comments

Comments

@lanmingle
Copy link

type Mapper struct {
	FindById         func(Id string) (string,error)
}

var swap = func(in []reflect.Value) []reflect.Value {
	fmt.Println(in)
	// code 1
	return []reflect.Value{in[0],reflect.ValueOf(nil)}
	// code 2
	//nvo := reflect.ValueOf(errors.New(""))
	//return []reflect.Value{in[0],nvo}
}
var makeSwap = func(fptr interface{}) {
	var valueOf reflect.Value = reflect.Indirect(reflect.ValueOf(fptr))
	var v reflect.Value = reflect.MakeFunc(valueOf.Type(), swap)
	valueOf.Set(v)
	fmt.Println(valueOf)
	fmt.Println(v)
}

code 1 : goroutine waiting for the (卡着不动了)
code 2:

panic: reflect: function created by MakeFunc using closure returned wrong type: have *errors.errorString for error

goroutine 1 [running]:
reflect.callReflect(0xc42000a0c0, 0xc420055e68)

normal:

func test6() {
	m := &Mapper{}
	makeSwap(&m.FindById)
	ss := m.FindById("1")
	fmt.Println(ss)
}

type Mapper struct {
	FindById         func(Id string) (string)
}

var swap = func(in []reflect.Value) []reflect.Value {
	fmt.Println(in)
	return []reflect.Value{in[0]}
}
var makeSwap = func(fptr interface{}) {
	var valueOf reflect.Value = reflect.Indirect(reflect.ValueOf(fptr))
	var v reflect.Value = reflect.MakeFunc(valueOf.Type(), swap)
	valueOf.Set(v)
	fmt.Println(valueOf)
	fmt.Println(v)
}
@lanmingle
Copy link
Author

这个我已相关的方式解决了。

@lanmingle
Copy link
Author

lanmingle commented Jul 5, 2018

http://siddontang.com/2014/02/21/golang-rpc-frame/

@golang golang locked and limited conversation to collaborators Nov 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants