Golang函数指针作为结构的一部分 [英] Golang function pointer as a part of a struct
问题描述
我有以下代码:
type FWriter struct {
WriteF func(p [] byte)(错误)
func(self * FWriter)Write(p [] byte)(n int,err error){
return self.WriteF(p )
}
func MyWriteFunction(p [] byte)(n int,err error){
//这个函数实现了Writer接口,但没有被命名为Write
fmt.Print(%v,p)
返回len(p),nil
}
MyFWriter:= new(FWriter)
MyFWriter .WriteF = MyWriteFunction
//我想用MyWriteFunction和io.Copy
io.Copy(MyFWriter,os.Stdin)
我试图做的是创建一个Writer接口来包装MyWriteFunction,因为它没有被命名为Write,我不能将它用于任何需要Writer接口的东西。
这个代码不会工作,因为它抱怨:
方法MyWriterFunction是不是表达式,必须称为
我在这里做错了什么?我怎样才能将WriteF设置为MyWriteFunction?
注意:我尽可能简化了这个问题,实际上我有一个具有MyWriteFunction和普通Write函数的结构所以它会变得有点复杂......(如果有更好的方法来解决我的这个问题,那么我很高兴听到它!)
谢谢! !
:我注意到我的错误并修复了它(MyWriterFunction - > MyWriteFunction)
我认为我过分简化了问题,误导了我的原意。
遵循匿名评论和peterSO友好评论,我重新创建了错误以更好地展示我的问题:
package主要
导入(
fmt
io
字符串
)
类型ProxyWrite接口{
Write(p [] byte)(n int,err error)
SpecialWrite(p [] byte)(n int,err error)
}
类型实现者struct {
counter int
}
func(self实施者)写(p []字节)(n int,err错误){
fmt.Print(正常写入:%v,p)
返回len(p),nil
}
func(自实现者)SpecialWrite(p [] byte)(n int,err错误){
fmt.Print(Normal write:%v \ n,p)
fmt.Println(And something else)
self.counter + = 1
返回len(p),nil
}
类型WriteFunc func(p []字节)(n int,err错误)
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)
/ *通常运行并使用Write方法* /
io.Copy(Proxies [1],strings.NewReader(Hello world))
/ *获取./main .go:45:方法Proxies [1] .SpecialWrite不是表达式,必须调用* /
io.Copy(WriteFunc(Proxies [1] .SpecialWrite),strings.NewReader(Hello world))
}
我希望它澄清我第一次尝试的意图。 p>
有什么想法?
无论如何,进入结构的func是不必要的。相反,您可以定义一个包装函数的WriteFunc类型,并且可以在其上定义一个Write方法。这是一个完整的例子。
包主
$ b导入(
fmt
io
字符串
)
类型WriteFunc func(p []字节)(n int,err错误)
func (wf WriteFunc)Write(p [] byte)(n int,err error){
return wf(p)
}
func myWrite(p [] byte)( n),错误错误){
fmt.Print(%v,p)
return len(p),nil
}
func main {
io.Copy(WriteFunc(myWrite),strings.NewReader(Hello world))
}
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\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"))
}
I hope it clarify what I meant to present on the first attempt.
any thoughts?
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"))
}
这篇关于Golang函数指针作为结构的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!