本文整理匯總了Golang中io.WriteCloser類的典型用法代碼示例。如果您正苦於以下問題:Golang WriteCloser類的具體用法?Golang WriteCloser怎麽用?Golang WriteCloser使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了WriteCloser類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: revc
func revc(conn net.Conn, w io.WriteCloser) {
defer w.Write([]byte("exit\n\r"))
defer conn.Close()
var b [100]byte
conn.Write([]byte("welcome\n"))
var s string
for {
n, err := conn.Read(b[0:100])
if err != nil {
break
}
//退格刪除字符
if b[0] == 8 {
s = s[0 : len(s)-1]
continue
}
s += string(b[0:n])
//回車提交
if b[n-1] == 10 {
//fmt.Print("接收:", n, s, b[1])
w.Write([]byte(s))
s = ""
}
}
}
示例2: Start
// Start the link with the specified toxics
func (link *ToxicLink) Start(name string, source io.Reader, dest io.WriteCloser) {
go func() {
bytes, err := io.Copy(link.input, source)
if err != nil {
logrus.WithFields(logrus.Fields{
"name": link.proxy.Name,
"upstream": link.proxy.Upstream,
"bytes": bytes,
"err": err,
}).Warn("Source terminated")
}
link.input.Close()
}()
for i, toxic := range link.toxics.chain {
go link.stubs[i].Run(toxic)
}
go func() {
bytes, err := io.Copy(dest, link.output)
if err != nil {
logrus.WithFields(logrus.Fields{
"name": link.proxy.Name,
"upstream": link.proxy.Upstream,
"bytes": bytes,
"err": err,
}).Warn("Destination terminated")
}
dest.Close()
link.toxics.RemoveLink(name)
link.proxy.RemoveConnection(name)
}()
}
示例3: PGPKeyRawToArmored
func PGPKeyRawToArmored(raw []byte, priv bool) (ret string, err error) {
var writer io.WriteCloser
var out bytes.Buffer
var which string
if priv {
which = "PRIVATE"
} else {
which = "PUBLIC"
}
hdr := fmt.Sprintf("PGP %s KEY BLOCK", which)
writer, err = armor.Encode(&out, hdr, PGPArmorHeaders)
if err != nil {
return
}
if _, err = writer.Write(raw); err != nil {
return
}
writer.Close()
ret = out.String()
return
}
示例4: saltpackSign
func saltpackSign(g *GlobalContext, source io.ReadCloser, sink io.WriteCloser, key NaclSigningKeyPair, streamer streamfn) error {
defer func() {
if err := source.Close(); err != nil {
g.Log.Warning("error closing source: %s", err)
}
if err := sink.Close(); err != nil {
g.Log.Warning("error closing sink: %s", err)
}
}()
stream, err := streamer(sink, saltSigner{key}, KeybaseSaltpackBrand)
if err != nil {
return err
}
if _, err := io.Copy(stream, source); err != nil {
return err
}
if err = stream.Close(); err != nil {
return err
}
return nil
}
示例5: Backup
//Backup - method to execute backup
func (s *MysqlPlugin) Backup() (err error) {
lo.G.Debug("Starting backup of mysql-tile")
var writer io.WriteCloser
var persistanceBackuper cfbackup.PersistanceBackup
var mysqlUserName, mysqlPassword string
var sshConfigs []command.SshConfig
if sshConfigs, err = s.getSSHConfig(); err == nil {
//take first node to execute backup on
sshConfig := sshConfigs[0]
if mysqlUserName, mysqlPassword, err = s.getMysqlCredentials(); err == nil {
lo.G.Debug("Successfully got mysqlCredentials")
if persistanceBackuper, err = s.GetPersistanceBackup(mysqlUserName, mysqlPassword, sshConfig); err == nil {
if writer, err = s.PivotalCF.NewArchiveWriter(outputFileName); err == nil {
defer writer.Close()
lo.G.Debug("Starting mysql dump")
err = persistanceBackuper.Dump(writer)
lo.G.Debug("Dump finished", err)
}
}
}
}
lo.G.Debug("Finished backup of mysql-tile", err)
return
}
示例6: HandleWrite
func HandleWrite(ctx syncer.Context, l uri.Uri, r uri.Uri) error {
if l.ModTime().Sub(r.ModTime()) < 0 {
return ctx.Finish()
}
var (
reader io.ReadCloser
writer io.WriteCloser
err error
)
for {
reader, err = l.OpenRead()
if err == nil {
break
}
time.Sleep(time.Second * 20)
}
for {
writer, err = r.OpenWrite()
if err == nil {
break
}
time.Sleep(time.Minute * 10)
}
defer reader.Close()
defer writer.Close()
_, err = io.Copy(writer, reader)
if err != nil {
ctx.EmitLog(syncer.TypeError, err)
}
ctx.EmitLog(syncer.TypeInfo, "write to ", r.Uri())
return err
}
示例7: migrateCommandFunc
func migrateCommandFunc(cmd *cobra.Command, args []string) {
var (
writer io.WriteCloser
reader io.ReadCloser
errc chan error
)
if migrateTransformer != "" {
writer, reader, errc = startTransformer()
} else {
fmt.Println("using default transformer")
writer, reader, errc = defaultTransformer()
}
st, index := rebuildStoreV2()
be := prepareBackend()
defer be.Close()
go func() {
writeStore(writer, st)
writer.Close()
}()
readKeys(reader, be)
mvcc.UpdateConsistentIndex(be, index)
err := <-errc
if err != nil {
fmt.Println("failed to transform keys")
ExitWithError(ExitError, err)
}
fmt.Println("finished transforming keys")
}
示例8: dump
func dump(args ...string) error {
var w io.WriteCloser
if len(args) == 0 {
w = hex.Dumper(os.Stdout)
} else {
var err error
w, err = os.Create(args[0])
if err != nil {
return err
}
}
defer w.Close()
d, err := openDevice()
if err != nil {
return err
}
defer d.Close()
if dumpCount == 0 {
dumpCount = eeprom.MaxBytes - dumpStart
}
data := make([]byte, dumpCount)
if err := d.Read(uint16(dumpStart), data); err != nil {
d.Reset()
return err
}
_, err = w.Write(data)
return err
}
示例9: fakeEngine
func fakeEngine(r io.Reader, w io.WriteCloser) {
buf := bufio.NewReader(r)
for {
line, _, err := buf.ReadLine()
if err != nil {
return
}
switch field := tokenise(string(line)); field.next() {
case "uci":
for _, o := range optionTests {
fmt.Fprintf(w, "option name %s type %s %s\n", o.name, o.typ, o.other)
}
fmt.Fprintln(w, "uciok")
case "isready":
fmt.Fprintln(w, "readyok")
case "setoption":
// ignore
case "go":
for _, i := range infoTests {
fmt.Fprintln(w, i.line)
}
case "quit":
w.Close()
return
}
}
}
示例10: Stream
func (c *Client) Stream(path string, headers map[string]string, in io.Reader, out io.WriteCloser) error {
origin := fmt.Sprintf("https://%s", c.Host)
endpoint := fmt.Sprintf("wss://%s%s", c.Host, path)
config, err := websocket.NewConfig(endpoint, origin)
if err != nil {
return err
}
config.TlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
config.Header.Set("Version", c.Version)
userpass := fmt.Sprintf("convox:%s", c.Password)
userpass_encoded := base64.StdEncoding.EncodeToString([]byte(userpass))
config.Header.Add("Authorization", fmt.Sprintf("Basic %s", userpass_encoded))
for k, v := range headers {
config.Header.Add(k, v)
}
config.TlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
var ws *websocket.Conn
if proxy := os.Getenv("HTTPS_PROXY"); proxy != "" {
ws, err = c.proxyWebsocket(config, proxy)
} else {
ws, err = websocket.DialConfig(config)
}
if err != nil {
return err
}
defer ws.Close()
var wg sync.WaitGroup
if in != nil {
go io.Copy(ws, in)
}
if out != nil {
wg.Add(1)
go copyAsync(out, ws, &wg)
}
wg.Wait()
out.Close()
return nil
}
示例11: write
func write(w io.WriteCloser) error {
if _, err := io.Copy(w, strings.NewReader(poem)); err != nil {
return err
}
return w.Close()
}
示例12: readWriterArchive
func (context *ElasticRuntime) readWriterArchive(dbInfo SystemDump, databaseDir string, action int) (err error) {
filename := fmt.Sprintf(ER_BACKUP_FILE_FORMAT, dbInfo.Get(SD_COMPONENT))
filepath := path.Join(databaseDir, filename)
var pb PersistanceBackup
if pb, err = dbInfo.GetPersistanceBackup(); err == nil {
switch action {
case IMPORT_ARCHIVE:
lo.G.Debug("we are doing something here now")
var backupReader io.ReadCloser
if backupReader, err = context.Reader(filepath); err == nil {
defer backupReader.Close()
err = pb.Import(backupReader)
}
case EXPORT_ARCHIVE:
lo.G.Info("Dumping database to file")
var backupWriter io.WriteCloser
if backupWriter, err = context.Writer(filepath); err == nil {
defer backupWriter.Close()
err = pb.Dump(backupWriter)
}
}
}
return
}
示例13: SniffClose
// ??
func SniffClose(name string, sink io.WriteCloser, source io.Reader, eof func()) {
sniffer := newSniffer(name)
z1, z2 := make(chan struct{}), make(chan struct{})
go func() {
io.Copy(sniffer, source)
sniffer.warn("SRC=>EVE⋅⋅SINK", "closing")
sniffer.Close()
close(z1)
}()
go func() {
io.Copy(sink, sniffer)
sniffer.warn("SRC⋅⋅EVE=>SINK", "closing")
sink.Close()
close(z2)
}()
go func() {
<-z1
sniffer.warn("SRC=>EVE⋅⋅SINK", "closed")
<-z2
sniffer.warn("SRC⋅⋅EVE=>SINK", "closed")
if eof != nil {
eof()
}
}()
}
示例14: inLoop
func inLoop(pipe io.WriteCloser, stdin io.Reader, done chan bool) {
buf := make([]byte, 2)
logger.Println("Entering stdin loop")
done <- true
for {
bytes_read, read_err := io.ReadFull(stdin, buf)
if read_err == io.EOF && bytes_read == 0 {
break
}
fatal_if(read_err)
length := read16_be(buf)
logger.Printf("in: packet length = %v\n", length)
if length == 0 {
// this is how Porcelain signals EOF from Elixir
break
}
bytes_written, write_err := io.CopyN(pipe, stdin, int64(length))
logger.Printf("in: copied %v bytes\n", bytes_written)
fatal_if(write_err)
}
pipe.Close()
done <- true
}
示例15: copyAndClose
func copyAndClose(wg *sync.WaitGroup, dest io.WriteCloser, src io.Reader) {
_, _ = io.Copy(dest, src)
_ = dest.Close()
if wg != nil {
wg.Done()
}
}