英文:
Make pointer from an unknown type
问题
是否可以创建一个等效的代码,而不提及特定类型(在这种情况下是SomeStruct)?
// 将SomeStruct{}转换为&SomeStruct{}并返回
func MakePointerFromStruct(someStruct interface{}) interface{} {
obj := someStruct.(SomeStruct)
return interface{}(&obj)
}
我已经在playground上创建了一个示例。
实际上,我的问题是我需要将来自会话的一些数据(我从Redis中获取的数据,其形式为User{})转换为&User{},但是操作该反序列化值的代码对其类型一无所知,并尝试将其转换为接口。
问题在于User{}上有一个指针方法用于修改数据。要将值转换为接口值,接口{}变量必须已经是指针:&User{}
以下是一些代码,以便更清楚地说明问题:
type User struct {}
func (u *User) FillFromOauth(name string) {
u.Name = name
//...
}
type UserInterface interface {
FillFromOauth(name string)
}
func someFuncWhereCastingOccurs(userObj interface{}) {
user := userObj.(UserInterface)
}
data := User{}
someFuncWhereCastingOccurs(data)
英文:
Is it possible to make an equivalent code to the following without mentioning a specific type (SomeStruct in this case)?
// takes SomeStruct{} and returns &SomeStruct{}
func MakePointerFromStruct(someStruct interface{}) interface{} {
obj := someStruct.(SomeStruct)
return interface{}(&obj)
}
I've created a playground
My actual problem is that I need to cast to &User{} some data from a session (which I get from redis and which is in a form User{}) but code which operates on that deserialized value doesn't know anything about its type and tries to cast it to an interface..
The problem is that there is one pointer method on User{} which modifies data. And to cast to an interface value in an interface{} variable must already be a pointer: &User{}
Some code to make it clear:
type User struct {}
func (u *User) FillFromOauth(name string) {
u.Name = name
//...
}
type UserInterface interface {
FillFromOauth(name string)
}
func someFuncWhereCastingOccurs(userObj interface{}) {
user := userObj.(UserInterface) //
}
data := User{}
someFuncWhereCastingOccurs(data)
答案1
得分: 1
以下是翻译好的内容:
import "reflect"
func MakePointerFromStruct(someStruct interface{}) interface{} {
ptrValue := reflect.New(reflect.TypeOf(someStruct))
reflect.Indirect(ptrValue).Set(reflect.ValueOf(someStruct))
return ptrValue.Interface()
}
类似这样的代码应该可以工作。这段代码的作用是将一个结构体转换为指针类型。
英文:
Something like this should work:
import "reflect"
func MakePointerFromStruct(someStruct interface{}) interface{} {
ptrValue := reflect.New(reflect.TypeOf(someStruct))
reflect.Indirect(ptrValue).Set(reflect.ValueOf(someStruct))
return ptrValue.Interface()
}
答案2
得分: 1
仅仅获取User{}
变量的地址有什么问题吗?
data := &User{}
someFuncWhereCastingOccurs(data)
请查看这个例子:http://play.golang.org/p/xYqfr5G4mw
顺便说一下,这不是类型转换,而是类型断言。
英文:
What's the problem on just taking address of your User{} variable?
data := &User{}
someFuncWhereCastingOccurs(data)
Take a look at this example: http://play.golang.org/p/xYqfr5G4mw
PS. By the way, it's not casting, it's type assertion.
答案3
得分: 0
一个指针可以通过以下方式转换为不安全的通用指针:
return unsafe.Pointer(&p)
然后,该值可以转换为您选择的任何指针类型。
英文:
A pointer can be converted to the type-unsafe generic pointer by
return unsafe.Pointer(&p)
That value can then be cast to whatever pointer type you choose.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论