英文:
How to assign a method to an existing struct object
问题
我想知道在Go语言中是否可以做类似这样的事情。
type MyStruct struct {
id int
}
func (ms *MyStruct) PrintHello() {
fmt.Printf("Hello from original method %v", ms.id)
}
func main() {
fmt.Println("Hello, playground")
m := MyStruct{}
m.PrintHello()
m.PrintHello = func() {fmt.Printf("Hello from newer method 2")}
}
错误:无法将值分配给m.PrintHello
https://play.golang.org/p/2oJQFFH4O5
如果对Go程序员来说这没有意义,我很抱歉,我对Go还不熟悉,想知道在Go中是否可以做一些在动态类型语言中可以做的事情。
<details>
<summary>英文:</summary>
I was wondering if doing something like this in Go is even possible.
type MyStruct struct {
id int
}
func (ms *MyStruct) PrintHello() {
fmt.Printf("Hello from original method %v", ms.id)
}
func main() {
fmt.Println("Hello, playground")
m := MyStruct{}
m.PrintHello()
m.PrintHello = func() {fmt.Printf("Hello from newer method 2")}
}
Error: cannot assign to m.PrintHello
https://play.golang.org/p/2oJQFFH4O5
Sorry if this doesn't make sense for Go programmers, I am new to Go and wondering if some of the things that can be done in dynamically typed languages can be done in Go.
</details>
# 答案1
**得分**: 15
鉴于Go是一种静态类型的语言,你不能执行这段特定的代码。然而,函数是变量,所以你可以做类似的事情。只要记住,从技术上讲,这与分配一个新的方法不同,正如JimB在评论中所述。
因为这个函数实际上不是一个方法,所以你只能通过将其作为参数传递来访问内部结构的值。这意味着如果它必须跨包边界,未导出的值将不可用。
还需要注意的是函数类型需要相同。所以如果你在结构中将函数定义为`PrintHello func(ms * MyStruct) error`,你需要分配一个返回错误的函数。
<details>
<summary>英文:</summary>
Given that Go is a statically typed language, you cannot do this specific piece of code. However, functions are variables, so you CAN do something like this. Just remember that this is technically NOT the same as assigning a new method, as JimB states in the comments.
https://play.golang.org/p/rfuCzXD8fP
package main
import (
"fmt"
)
type MyStruct struct {
id int
PrintHello func(ms * MyStruct)
}
func (ms *MyStruct) init() {
ms.PrintHello = func(ms *MyStruct) { fmt.Printf("Hello from original method %v\n", ms.id) }
}
func main() {
m := &MyStruct{id: 42}
m.init()
m.PrintHello(m)
m.PrintHello = func(ms *MyStruct) { fmt.Printf("Hello from newer method 2 %d\n", ms.id) }
m.PrintHello(m)
}
Because this function is not really a method, you can only access the internal struct values by passing it as an argument. This means that if this ever has to cross package boundaries, unexported values will not be available.
It is also important to note that the function type needs to be the same. So if you defined the function as `PrintHello func(ms * MyStruct) error` in the struct, you would need to assign a function that returns an error.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论