英文:
Anonymous function on struct
问题
在Go语言中,你可以使用匿名函数来更新结构体中的值。以下是一个示例:
type Info struct {
Inspect func(string) string
}
func Assign() Info {
i := &Info{}
i.Inspect = func(id string) string {
return fmt.Sprintf("/api/%s/inspect", id)
}
return *i
}
func main() {
temp := Assign()
tempID := temp.Inspect("test")
fmt.Println(tempID)
}
在这个示例中,我们在Info
结构体中定义了一个函数类型的字段Inspect
,该字段接受一个字符串参数并返回一个字符串。在Assign
函数中,我们创建了一个Info
类型的实例,并将Inspect
字段赋值为一个匿名函数,该匿名函数使用传入的id
参数构建字符串并返回。最后,在main
函数中,我们调用temp.Inspect
函数并传入"test"
作为参数,将返回的结果打印出来。
输出结果将是/api/test/inspect
。
英文:
Is it possible to update a value on in a struct using an anonymous function? In python I would do the following with a lambda:
inspect = lambda id: '/api/{}/inspect'.format(id)
Which would place the dynamic id
value in the string.
In Go
I am trying something like his:
type Info struct {
Inspect string
}
func Assign() Info {
i := &Info{}
i.Inspect = return func(id) {return fmt.Sprintf("/api/%s/inspect", id)}
return *i
}
But I want to update the value like this:
temp := Assign()
tempID := temp.Inspect("test")
fmt.Println("/api/test/inspect")
答案1
得分: 6
Go是静态类型的,而Python是动态类型的。这意味着在Go中,你必须为每个变量声明(或让编译器推断)一个类型,并且它必须始终保持该类型。因此,你不能将Inspect
属性(类型为string
)赋值为一个lambda函数。
根据你的问题,不太清楚你希望它如何工作。以下是一个示例,展示了你可以做的事情:
type Info struct {
Inspect func(string) string
}
func Assign() Info {
i := Info{
Inspect: func(id string) string { return fmt.Sprintf("/api/%s/inspect", id) },
}
return i
}
func main() {
temp := Assign()
tempID := temp.Inspect("test")
fmt.Println(tempID)
}
没有必要将i
声明为指向Info
的指针,然后返回它的值。使用其中之一即可。
你可以在这里的playground上查看示例。
英文:
Go is statically typed, where python is dynamically typed. This means that in Go, you must declare (or let the compiler infer) a type for each variable, and it must always stay that type. Therefore, you cannot assign your Inspect
property (typed as a string
) as a lambda function.
Based on your question, it's not entirely clear how exactly you want this to work. Here is an example of what you could do, though:
type Info struct {
Inspect func(string) string
}
func Assign() Info {
i := Info{
Inspect: func(id string) string { return fmt.Sprintf("/api/%s/inspect", id) },
}
return i
}
You can then use it like so:
temp := Assign()
tempID := temp.Inspect("test")
fmt.Println(tempID)
There is also no need to declare i
as a pointer to Info
and then return the value of it. Use one or the other.
Here it is on the playground.
答案2
得分: 2
你可以将匿名函数定义为结构体的属性,但类型必须是完整的函数签名,而不仅仅是函数的返回值。
type Info struct {
Inspect func(string) string
}
func Assign() Info {
i := &Info{}
i.Inspect = func(id string) string {
return fmt.Sprintf("/api/%s/inspect", id)
}
return *i
}
func main() {
temp := Assign()
tempID := temp.Inspect("test")
fmt.Println(tempID)
}
你可以在这里进行尝试:https://play.golang.org/p/IRylodv2cZ
英文:
You can certainly define an anonymous function as a property of a struct, but the type has to be the full function signature, rather than just the return value of the function.
type Info struct {
Inspect func(string) string
}
func Assign() Info {
i := &Info{}
i.Inspect = func(id string) string {
return fmt.Sprintf("/api/%s/inspect", id)
}
return *i
}
func main() {
temp := Assign()
tempID := temp.Inspect("test")
fmt.Println(tempID)
}
Feel free to play around with that here: https://play.golang.org/p/IRylodv2cZ
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论