如何将方法分配给现有的结构对象?

huangapple go评论67阅读模式
英文:

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(&quot;Hello from original method %v&quot;, ms.id)
    }
    
    func main() {
    	fmt.Println(&quot;Hello, playground&quot;)
    	m := MyStruct{}
    	m.PrintHello()
    	
    	m.PrintHello = func() {fmt.Printf(&quot;Hello from newer method 2&quot;)}
    }

    Error: cannot assign to m.PrintHello

https://play.golang.org/p/2oJQFFH4O5

Sorry if this doesn&#39;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 (
    	&quot;fmt&quot;
    )

    type MyStruct struct {
    	id         int
    	PrintHello func(ms * MyStruct)
    }

    func (ms *MyStruct) init() {
    	ms.PrintHello = func(ms *MyStruct) { fmt.Printf(&quot;Hello from original method %v\n&quot;, ms.id) }
    }

    func main() {
    	m := &amp;MyStruct{id: 42}
    	m.init()
    	m.PrintHello(m)

    	m.PrintHello = func(ms *MyStruct) { fmt.Printf(&quot;Hello from newer method 2 %d\n&quot;, 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>



huangapple
  • 本文由 发表于 2017年4月1日 03:07:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/43147973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定