本文整理汇总了Golang中io.WriterTo.WriteTo方法的典型用法代码示例。如果您正苦于以下问题:Golang WriterTo.WriteTo方法的具体用法?Golang WriterTo.WriteTo怎么用?Golang WriterTo.WriteTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.WriterTo
的用法示例。
在下文中一共展示了WriterTo.WriteTo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Send
func (c *smtpSender) Send(from string, to []string, msg io.WriterTo) error {
if err := c.Mail(from); err != nil {
if err == io.EOF {
// This is probably due to a timeout, so reconnect and try again.
sc, derr := c.d.Dial()
if derr == nil {
if s, ok := sc.(*smtpSender); ok {
*c = *s
return c.Send(from, to, msg)
}
}
}
return err
}
for _, addr := range to {
if err := c.Rcpt(addr); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
if _, err = msg.WriteTo(w); err != nil {
w.Close()
return err
}
return w.Close()
}
示例2: doV3n11
// Redirect -- only accessible if method == POST
func (p *wmDecisionCore) doV3n11() WMDecision {
var postIsCreate bool
var httpCode int
var httpError error
var httpHeaders http.Header
var writerTo io.WriterTo
postIsCreate, p.req, p.cxt, httpCode, httpError = p.handler.PostIsCreate(p.req, p.cxt)
if httpCode > 0 {
p.writeHaltOrError(httpCode, httpError)
return wmResponded
}
if postIsCreate {
log.Print("[WM]: v3n11: Creating Path\n")
_, p.req, p.cxt, httpCode, httpError = p.handler.CreatePath(p.req, p.cxt)
if httpCode > 0 {
p.writeHaltOrError(httpCode, httpError)
return wmResponded
}
log.Print("[WM]: v3n11: Running Accept Helper\n")
if p.runAcceptHelper() {
return wmResponded
}
} else {
log.Print("[WM]: v3n11: Running Process Post\n")
p.req, p.cxt, httpCode, httpHeaders, writerTo, httpError = p.handler.ProcessPost(p.req, p.cxt)
if httpCode > 0 {
p.updateHttpResponseHeaders(httpHeaders)
if httpError != nil {
p.writeHaltOrError(httpCode, httpError)
} else if writerTo != nil {
p.resp.WriteHeader(httpCode)
writerTo.WriteTo(p.resp)
} else {
p.resp.WriteHeader(httpCode)
}
return wmResponded
}
// TODO Aalok check what should be done here
//p.resp.WriteHeader(n)
//log.Print("Wrote Header but may not return wmResponded in doV3n11()\n")
//p.encodeBodyIfSet()
}
log.Print("[WM]: v3n11: Running Response is Redirect?\n")
var respIsRedirect bool
respIsRedirect, p.req, p.cxt, httpCode, httpError = p.handler.ResponseIsRedirect(p.req, p.cxt)
if httpCode > 0 {
p.writeHaltOrError(httpCode, httpError)
return wmResponded
}
if respIsRedirect {
if len(p.resp.Header().Get("Location")) > 0 {
p.resp.WriteHeader(http.StatusSeeOther)
} else {
p.resp.WriteHeader(http.StatusInternalServerError)
io.WriteString(p.resp, "Response had do_redirect but no Location")
}
return wmResponded
}
return v3p11
}
示例3: main
func main() {
flag.Parse()
fname := flag.Arg(0)
if fname == "" {
usage()
os.Exit(1)
}
f, err := os.Create(fname)
if err != nil {
log.Fatalf("%s", err)
}
defer f.Close()
var w io.WriterTo
d := captcha.RandomDigits(*flagLen)
switch {
case *flagAudio:
w = captcha.NewAudio(d, *flagLang)
case *flagImage:
w = captcha.NewImage(d, *flagImgW, *flagImgH)
}
_, err = w.WriteTo(f)
if err != nil {
log.Fatalf("%s", err)
}
fmt.Println(d)
}
示例4: stringifyWriterTo
func stringifyWriterTo(w io.WriterTo) (string, error) {
var buf bytes.Buffer
_, err := w.WriteTo(&buf)
if err != nil {
return "", err
}
return buf.String(), nil
}
示例5: writeFile
func writeFile(dst string, src io.WriterTo) (int64, error) {
f, err := os.Create(dst)
if err != nil {
return 0, fmt.Errorf("error creating file %q: %v", dst, err)
}
defer fi.SafeClose(f)
return src.WriteTo(f)
}
示例6: writeTo
func writeTo(w io.Writer, wt io.WriterTo, n *int64, err *error) {
if *err != nil {
return
}
var nn int64
nn, *err = wt.WriteTo(w)
*n += nn
}
示例7: offlineSend
func offlineSend(from string, to []string, msg io.WriterTo) error {
if OfflineLogger.IsInfo() {
var buf bytes.Buffer
if _, err := msg.WriteTo(&buf); err != nil {
return log.Error("mail.daemon.OfflineSend", "err", err, "buf", buf.String(), "message", msg)
}
OfflineLogger.Info("mail.Send", "from", from, "to", to, "msg", buf.String())
}
return nil
}
示例8: writeToFile
// writeToFile creates the specified output file from the contents of w.
func writeToFile(ctx context.Context, w io.WriterTo, path string) error {
f, err := os.Create(path)
if err != nil {
return err
}
if _, err := w.WriteTo(f); err != nil {
f.Close()
return err
}
return f.Close()
}
示例9: Send
func (msc mockSendCloser) Send(from string, to []string, msg io.WriterTo) error {
assert.NotEmpty(msc.t, from)
assert.NotEmpty(msc.t, to)
var buf bytes.Buffer
msg.WriteTo(&buf)
assert.NotEmpty(msc.t, buf.String())
//msc.t.Log(buf.String())
return msc.sendErr
}
示例10: Deliver
func (s *SMTPDeliverer) Deliver(ctx scope.Context, from, to string, email io.WriterTo) error {
// Connect and authenticate to SMTP server.
c, err := smtp.Dial(s.addr)
if err != nil {
return fmt.Errorf("%s: dial error: %s", s, err)
}
defer c.Quit()
if err := c.Hello(s.localName); err != nil {
return fmt.Errorf("%s: ehlo error: %s", s, err)
}
if s.tlsConfig != nil {
if err := c.StartTLS(s.tlsConfig); err != nil {
return fmt.Errorf("%s: starttls error: %s", s, err)
}
}
if s.auth != nil {
if err := c.Auth(s.auth); err != nil {
return fmt.Errorf("%s: auth error: %s", s, err)
}
}
// Send email.
if from == "" {
from = "[email protected]" + s.localName
}
if err := c.Mail(from); err != nil {
return fmt.Errorf("%s: mail error: %s", s, err)
}
if err := c.Rcpt(to); err != nil {
return fmt.Errorf("%s: rcpt error: %s", s, err)
}
wc, err := c.Data()
if err != nil {
return fmt.Errorf("%s: data error: %s", s, err)
}
if _, err := email.WriteTo(wc); err != nil {
return fmt.Errorf("%s: write error: %s", s, err)
}
if err := wc.Close(); err != nil {
return fmt.Errorf("%s: close error: %s", s, err)
}
return nil
}
示例11: Deliver
func (td *TestDeliverer) Deliver(ctx scope.Context, from, to string, email io.WriterTo) error {
td.Lock()
defer td.Unlock()
buf := &bytes.Buffer{}
if _, err := email.WriteTo(buf); err != nil {
return err
}
if ch, ok := td.channels[to]; ok {
ch <- buf.Bytes()
} else {
fmt.Printf("delivered:\n%s\n", buf.String())
}
return nil
}
示例12: gen
func gen(fname string) (Id string) {
f, err := os.Create(fname)
if err != nil {
log.Fatalf("%s", err)
}
defer f.Close()
var w io.WriterTo
d := captcha.RandomDigits(6) // six digits is enough
w = captcha.NewImage(d, captcha.StdWidth, captcha.StdHeight)
_, err = w.WriteTo(f)
if err != nil {
log.Fatalf("%s", err)
}
log.Println("DEBUG Captcha: Created new image ", d)
str := trans(d)
return str
}
示例13: stats
func stats(w io.WriterTo, regex string, h *generic.Histogram) (sum, final float64) {
re := regexp.MustCompile(regex)
buf := &bytes.Buffer{}
w.WriteTo(buf)
//fmt.Fprintf(os.Stderr, "%s\n", buf.String())
s := bufio.NewScanner(buf)
for s.Scan() {
match := re.FindStringSubmatch(s.Text())
f, err := strconv.ParseFloat(match[1], 64)
if err != nil {
panic(err)
}
sum += f
final = f
if h != nil {
h.Observe(f)
}
}
return sum, final
}
示例14: WriteToPathExclusive
func WriteToPathExclusive(path string, source io.WriterTo, mode os.FileMode) error {
file, exists, err := OpenFileExclusive(path, mode)
if err != nil {
return err
}
if exists {
if _, err := file.Seek(0, 0); err != nil {
file.Close()
return err
}
if err := file.Truncate(0); err != nil {
file.Close()
return err
}
}
if _, err := source.WriteTo(file); err != nil {
file.Close()
return err
}
return file.Close()
}
示例15: HandlePageRequestById
func (serv *Server) HandlePageRequestById(w http.ResponseWriter, r *http.Request) {
m := draftPageReg.FindStringSubmatch(r.URL.Path)
if m == nil {
http.Error(w, "Not found", 404)
return
}
id := m[1]
fmt.Println("req by id", id)
var d io.WriterTo
var ok bool
d, ok = serv.activeDrafts[id]
if !ok {
d, ok = serv.archive.GetDraft(id)
}
if !ok {
//404
}
_, err := d.WriteTo(w)
if err != nil {
//server error
}
}