【发布时间】:2017-10-19 08:11:16
【问题描述】:
我不明白错误,这是我在机器“A”中执行的 main.go:
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.\n", err.Error())
}
// ReceiveSnapshot
ds, err := zfs.ReceiveSnapshot(buff, "tank/replication")
if err != nil {
fmt.Printf("Error receiving: %s.\n", err.Error())
}
fmt.Printf("%s... done!\n", ds)
// Send a response back to person contacting us.
conn.Write([]byte("Received!"))
// Close the connection when you're done with it.
conn.Close()
}
现在,我向您展示来自 github.com/mistifyio/go-zfs/zfs.go 的函数 ReceiveSnapshot:
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)
}
我在 golang pkg 中看到过 io.Reader 的文档:
type Reader interface {
Read(p []byte) (n int, err error)
}
为什么我会收到错误消息...
- 不能在 zfs.ReceiveSnapshot 的参数中使用 buff(类型 []byte)作为类型 io.Reader: []byte 没有实现 io.Reader(缺少 Read 方法)
...当我创建go install 时?
【问题讨论】:
-
错误信息有什么不清楚的地方? []byte 没有实现 io.Reader 的 Read 方法,因此不能用作 io.Reader。请完成 Tour of Go 的方法和接口章节(从这里开始:tour.golang.org/methods/1)
-
我知道信息很清楚,但如果我问这个,那是因为我不明白(而且我已经阅读了教程......)
buff := make([]byte)和 Read 方法 @987654327 之间的区别@