douguanyun2169 2017-05-19 09:11
浏览 940
已采纳

无法将([] byte类型)用作io.Reader类型

I don't understand the error, this is my main.go that I execute in the machine "A":

package main

import (
    "fmt"
    "net"
    "os"
    "github.com/mistifyio/go-zfs"
)

func main() {
    // Listen for incoming connections.
    l, err := net.Listen("tcp", "192.168.99.5:9977")
    if err != nil ...
    // Close the listener when the application closes.
    defer l.Close()
    fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
    for {
        // Listen for an incoming connection.
        conn, err := l.Accept()
        if err != nil ...

        //Handle connections in a new goroutine.
        go handleRequest(conn)
    }
}

// Handles incoming requests.
func handleRequest(conn net.Conn) {
    // Make a buffer to hold incoming data.
    buff := make([]byte, 1024)
    // Read the incoming connection into the buffer.
    _, err := conn.Read(buff)
    if err != nil {
        fmt.Printf("Error reading: %s.
", err.Error())
    }
    // ReceiveSnapshot
    ds, err := zfs.ReceiveSnapshot(buff, "tank/replication")
    if err != nil {
        fmt.Printf("Error receiving: %s.
", err.Error())
    }
    fmt.Printf("%s... done!
", ds)
    // Send a response back to person contacting us.
    conn.Write([]byte("Received!"))
    // Close the connection when you're done with it.
    conn.Close()
}

Now, I show you the function ReceiveSnapshot from github.com/mistifyio/go-zfs/zfs.go:

type command struct {
    Command string
    Stdin   io.Reader
    Stdout  io.Writer
}

func ReceiveSnapshot(input io.Reader, name string) (*Dataset, error) {
    c := command{Command: "zfs", Stdin: input}
    _, err := c.Run("receive", name)
    if err != nil {
        return nil, err
    }
    return GetDataset(name)
}

I have seen in golang pkg the doc of io.Reader:

type Reader interface {
        Read(p []byte) (n int, err error)
}

Why do I receive the error...

  • cannot use buff (type []byte) as type io.Reader in argument to zfs.ReceiveSnapshot: []byte does not implement io.Reader (missing Read method)

...when I make go install ?

  • 写回答

2条回答 默认 最新

  • doulun5683 2017-05-19 12:23
    关注

    I think you are missing a step in your logic when you think that []byte would be equivalent to Reader just because the Reader's Read method receives a []byte as a parameter.

    Let me try to clarify:

    Your ReceiveSnapshot function expects a Reader as a parameter:

    ReceiveSnapshot( input io.Reader ...
    

    In order for a type to fulfil the Reader interface, that type should itself implement this function:

    Read(p []byte) (n int, err error)
    

    Note that the type should implement that function in order to be a Reader.

    []byte does not implement a Read function. It is just a coincidence that the argument to Read happens to be a []byte.

    In order for this to work, you need to send ReceiveSnapshot a proper Reader.

    Luckily for you, having a []byte and wanting to Read for it is a common situation so the API provides an easy way to do this:

    https://golang.org/pkg/bytes/#NewReader

    You just need to send bytes.NewReader(buff) to your ReceiveSnapshot function instead of just buff.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多