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

@lanmingle lanmingle commented Jul 5, 2018

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

This comment has been minimized.

Copy link
Author

@lanmingle lanmingle commented Jul 5, 2018

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

@lanmingle

This comment has been minimized.

Copy link
Author

@lanmingle lanmingle commented Jul 5, 2018

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

@lanmingle lanmingle closed this Jul 5, 2018
@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
2 participants
You can’t perform that action at this time.