本文整理汇总了Golang中io.PipeReader.CloseWithError方法的典型用法代码示例。如果您正苦于以下问题:Golang PipeReader.CloseWithError方法的具体用法?Golang PipeReader.CloseWithError怎么用?Golang PipeReader.CloseWithError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.PipeReader
的用法示例。
在下文中一共展示了PipeReader.CloseWithError方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: secWriteLoop
// secWriteLoop copies data from pr into w
// doing a nacl seal encryption on the data in the process using shared as the key
func secWriteLoop(w io.Writer, pr *io.PipeReader, shared *[32]byte) {
var failed bool
// check for an error, stops the loop and
// closes the pipe with err to signal the writer we failed
var check = func(err error) {
if err != nil {
log.Println("secWriteLoop err:", err)
if err2 := pr.CloseWithError(err); err2 != nil {
log.Println("CloseWithError failed", err2)
}
failed = true
}
}
for !failed { // until an error occurs
// read the clear message from our pipe
msg := make([]byte, 1024)
n, err := pr.Read(msg)
check(err)
// cut of the unused bytes
msg = msg[:n]
// read 24 bytes of random for our nonce
var nonce [24]byte
_, err = io.ReadFull(rand.Reader, nonce[:])
check(err)
// encrypt and sign our message with the prepended nonce
buf := box.SealAfterPrecomputation(nonce[:], msg, &nonce, shared)
// copy the sealed message with our passed writer
_, err = io.Copy(w, bytes.NewReader(buf))
check(err)
}
}
示例2: Write
func (w *imageProgressWriter) Write(data []byte) (int, error) {
w.mutex.Lock()
defer w.mutex.Unlock()
if w.internalWriter == nil {
var pipeIn *io.PipeReader
pipeIn, w.internalWriter = io.Pipe()
decoder := json.NewDecoder(pipeIn)
go func() {
err := w.readProgress(decoder)
if err != nil {
pipeIn.CloseWithError(err)
}
}()
}
return w.internalWriter.Write(data)
}
示例3: HandleWrite
func HandleWrite(filename string, r *io.PipeReader) {
r.CloseWithError(fmt.Errorf("Server is Read Only"))
}