douxu0550 2013-12-22 11:36
浏览 51
已采纳

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

I have the following code:

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)

What I am trying to do is to create a Writer interface to wrap MyWriteFunction because it is not named "Write" and I cant use it with anything that require a "Writer" interface.

this code wont work as it complains:

method MyWriterFunction is not an expression, must be called

what am I doing wrong here? how can I set WriteF to be MyWriteFunction?

Note: I simplified this problem as much as I can and in reality I have a struct which have MyWriteFunction AND a normal Write function so it gets a little bit complicated... (also if there is a better way to solve this problem of mine then Ill be glad to hear it!)

Thanks!!


EDIT:: I have notice my typo and fixed it (MyWriterFunction --> MyWriteFunction)

I think I over-simplified the question in a manner that mislead you of my original intent. Following the Anonymous comment and peterSO kind comments I have re-created the error to better demonstrate my problem:

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
", 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"))
}

I hope it clarify what I meant to present on the first attempt.

any thoughts?

  • 写回答

2条回答 默认 最新

  • dongwei3866 2013-12-22 13:04
    关注

    There's a typo in your code, but wrapping the func into a struct is unnecessary anyway. Instead, you can just define a WriteFunc type that wraps a function, and that you can define a Write method on. Here's a full example.

    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条)

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?