go-ipfs-cmd

使用通道接收数据

1.通道的收发操作在不同的两个 goroutine 间进行。由于通道的数据在没有接收方处理时,数据发送方会持续阻塞,因此通道的接收必定在另外一个 goroutine 中进行。

2.接收将持续阻塞直到发送方发送数据。如果接收方接收时,通道中没有发送方发送数据,接收方也会发生阻塞,直到发送方发送数据为止。

3.每次接收一个元素。通道一次只能接收一个数据元素。

 

通道的数据接收一共有以下 4 种写法。

1.阻塞模式接收数据时

data := <-ch

2.使用非阻塞方式从通道接收数据时,语句不会发生阻塞

data, ok := <-ch

3.接收任意数据,忽略接收的数据

<-ch

4.通道的数据接收可以借用 for range 语句进行多个元素的接收操作

for data := range ch {
}

 

 

下面是关于go-ipfs-cmds

基本语法 Semantics 

基本构建块是 requests, commands, emitters and responses。在这个包和子包中有许多emitters ,但是用户可以自由地创建自己的发射器。

The basic building blocks are requests, commands, emitters and responses. A command consists of a description of the parameters and a function. The function is passed the request as well as an emitter as arguments. It does operations on the inputs and sends the results to the user by emitting them.

There are a number of emitters in this package and subpackages, but the user is free to create their own.

命令 Commands 

命令是一个结构体,它包含命令帮助文本、参数和选项的描述、命令的处理函数以及让调用者知道将发出什么类型的类型。可以选择定义一个函数PostRun和编码器,该函数使用函数发出的值,并为例如终端生成可视化表示。编码器按价值工作,而PostRun按价值流工作。

A command is a struct containing the commands help text, a description of the arguments and options, the command's processing function and a type to let the caller know what type will be emitted. Optionally one of the functions PostRun and Encoder may be defined that consumes the function's emitted values and generates a visual representation for e.g. the terminal. Encoders work on a value-by-value basis, while PostRun operates on the value stream.

发射器 Emitters

发射器具有Emit方法,该方法将命令函数的输出作为参数传递给用户。

An emitter has the Emit method, that takes the command's function's output as an argument and passes it to the user.

type ResponseEmitter interface {
	io.Closer
	SetLength(length uint64)
	SetError(err interface{}, code ErrorType)
	Emit(value interface{}) error
}

该命令的函数不知道它与哪种发射器一起工作,因此相同的函数可以使用rpc接口在本地或服务器上运行。发射器也可以使用SetError方法发送错误。
面向用户的发射器通常是cli发射器。这里的值发射器将使用编码器或PostRun函数打印到终端。

The command's function does not know what kind of emitter it works with, so the same function may run locally or on a server, using an rpc interface. Emitters can also send errors using the SetError method.

The user-facing emitter usually is the cli emitter. Values emitter here will be printed to the terminal using either the Encoders or the PostRun function.

回应 Responses 

响应是用户可以从中读取已发出值的值。

A response is a value that the user can read emitted values from.

type Response interface {
	Request() Request
	Error() *Error
	Length() uint64
	Next() (interface{}, error)
}

响应有一个Next()方法,该方法返回下一个发出的值和一个错误值。如果接收到最后一个元素,返回的错误值是io.EOF。如果应用程序代码使用SetError发送了一个错误,错误ErrRcvdError将在next返回,指示调用者应该调用error()。根据响应类型的不同,还可能发生其他错误。

Responses have a method Next() that returns the next emitted value and an error value. If the last element has been received, the returned error value is io.EOF. If the application code has sent an error using SetError, the error ErrRcvdError is returned on next, indicating that the caller should call Error(). Depending on the reponse type, other errors may also occur.

管道 Pipes 

管道是成对的(发射器、响应),因此可以在响应值中接收发射器上发出的值。大多数内置发射器都是“管道”发射器。最突出的例子是通道管道和http管道。
通道管道由通道支撑。响应返回的惟一错误值是io。EOF,当通道关闭时发生。
http管道由http连接支持。响应还可以返回其他错误,例如,如果网络上有错误。

Pipes are pairs (emitter, response), such that a value emitted on the emitter can be received in the response value. Most builtin emitters are "pipe" emitters. The most prominent examples are the channel pipe and the http pipe.

The channel pipe is backed by a channel. The only error value returned by the response is io.EOF, which happens when the channel is closed.

The http pipe is backed by an http connection. The response can also return other errors, e.g. if there are errors on the network.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值