Golang函数指针作为结构的一部分

时间:2013-12-22 11:36:37

标签: function interface go writer

我有以下代码:

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 个答案:

答案 0 :(得分:16)

您的代码中存在拼写错误,但无论如何都不需要将func包装到结构中。相反,您可以只定义包装函数的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"))
}

答案 1 :(得分:6)

修复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)
}