Golang结构体中的函数指针

20

我有以下的代码:

type FWriter struct {
    WriteF func(p []byte) (n int,err error)
}

func (self *FWriter) Write(p []byte) (n int, err error) {
    return self.WriteF(p)
}

func MyWriteFunction(p []byte) (n int, err error) { 
    // this function implements the Writer interface but is not named "Write"
    fmt.Print("%v",p)
    return len(p),nil
}

MyFWriter := new(FWriter)
MyFWriter.WriteF = MyWriteFunction
// I want to use MyWriteFunction with io.Copy
io.Copy(MyFWriter,os.Stdin)
我想做的是创建一个Writer接口来包装MyWriteFunction,因为它没有命名为“Write”,所以我无法将其与需要“Writer”接口的任何内容一起使用。
以下代码会报错:
method MyWriterFunction is not an expression, must be called
我在这里做错了什么?如何设置WriteF为MyWriteFunction?
注意:我尽可能地简化了这个问题,在实际情况中,我有一个结构体,其中有MyWriteFunction和一个普通的Write函数,所以它变得有点复杂...(如果有更好的解决方法,我会很乐意听到!)
谢谢!!
编辑:我注意到了我的拼写错误并进行了更正(MyWriterFunction --> MyWriteFunction)。
我认为我过于简化了问题,以至于误导了您对我的原始意图。根据匿名评论和peterSO的友好评论,我重新创建了错误,以更好地演示我的问题:
package main

import (
    "fmt"
    "io"
    "strings"
)

type ProxyWrite interface {
    Write(p []byte) (n int, err error)
    SpecialWrite(p []byte) (n int, err error)
}

type Implementer struct {
    counter int
}

func (self Implementer) Write(p []byte) (n int, err error) {
    fmt.Print("Normal write: %v", p)
    return len(p),nil
}

func (self Implementer) SpecialWrite(p []byte) (n int, err error) {
    fmt.Print("Normal write: %v\n", p)
    fmt.Println("And something else")
    self.counter += 1
    return len(p),nil
}


type WriteFunc func(p []byte) (n int, err error)

func (wf WriteFunc) Write(p []byte) (n int, err error) {
    return wf(p)
}

func main() {
    Proxies := make(map[int]ProxyWrite,2)
    Proxies[1] = new(Implementer)
    Proxies[2] = new(Implementer)

    /* runs and uses the Write method normally */
    io.Copy(Proxies[1], strings.NewReader("Hello world"))
    /* gets ./main.go:45: method Proxies[1].SpecialWrite is not an expression, must be called */
    io.Copy(WriteFunc(Proxies[1].SpecialWrite), strings.NewReader("Hello world"))
}

我希望这能澄清我在第一次尝试中想要呈现的内容。

有什么想法吗?


2
你使用的是哪个版本的Go?方法作为值直到1.1才出现。而且这段代码在playground上运行良好... http://play.golang.org/p/AmRi_fujPw - Paul Hankin
2个回答

19

你的代码中有拼写错误,但将函数包装到结构体中是不必要的。相反,您可以定义一个包装函数的WriteFunc类型,并且您可以在其上定义一个Write方法。这是一个完整的例子。

package main

import (
    "fmt"
    "io"
    "strings"
)

type WriteFunc func(p []byte) (n int, err error)

func (wf WriteFunc) Write(p []byte) (n int, err error) {
    return wf(p)
}

func myWrite(p []byte) (n int, err error) {
    fmt.Print("%v", p)
    return len(p), nil
}

func main() {
    io.Copy(WriteFunc(myWrite), strings.NewReader("Hello world"))
}

你好,这个答案非常好,而且运行良好。我已经编辑了问题,以更好地解释我最初的意思。如果您不介意看一下,那就太棒了。谢谢! - user340495

7

修正MyWriterFunction/MyWriteFunction的拼写错误。例如,

package main

import (
    "fmt"
    "io"
    "os"
)

type FWriter struct {
    WriteF func(p []byte) (n int, err error)
}

func (self *FWriter) Write(p []byte) (n int, err error) {
    return self.WriteF(p)
}

func MyWriteFunction(p []byte) (n int, err error) {
    // this function implements the Writer interface but is not named "Write"
    fmt.Print("%v", p)
    return len(p), nil
}

func main() {
    MyFWriter := new(FWriter)
    MyFWriter.WriteF = MyWriteFunction
    // I want to use MyWriteFunction with io.Copy
    io.Copy(MyFWriter, os.Stdin)
}

嘿,感谢您的友善回答,它确实解决了所提出的问题,但我现在已经修正了我的问题以更好地描述问题。再次感谢您的友善回答。 - user340495

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接