mirror of
https://github.com/golang/go
synced 2024-11-26 14:08:37 -07:00
net/mail: add example to package
Change-Id: I912cafc66463f81cde839afc8f06b7eadcbf6f57 Reviewed-on: https://go-review.googlesource.com/11992 Reviewed-by: Andrew Gerrand <adg@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
f04c506917
commit
9f70cd8223
77
src/net/mail/example_test.go
Normal file
77
src/net/mail/example_test.go
Normal file
@ -0,0 +1,77 @@
|
||||
// Copyright 2015 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package mail_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/mail"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ExampleParseAddressList() {
|
||||
const list = "Alice <alice@example.com>, Bob <bob@example.com>, Eve <eve@example.com>"
|
||||
emails, err := mail.ParseAddressList(list)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, v := range emails {
|
||||
fmt.Println(v.Name, v.Address)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Alice alice@example.com
|
||||
// Bob bob@example.com
|
||||
// Eve eve@example.com
|
||||
}
|
||||
|
||||
func ExampleParseAddress() {
|
||||
e, err := mail.ParseAddress("Alice <alice@example.com>")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(e.Name, e.Address)
|
||||
|
||||
// Output:
|
||||
// Alice alice@example.com
|
||||
}
|
||||
|
||||
func ExampleReadMessage() {
|
||||
msg := `Date: Mon, 23 Jun 2015 11:40:36 -0400
|
||||
From: Gopher <from@example.com>
|
||||
To: Another Gopher <to@example.com>
|
||||
Subject: Gophers at Gophercon
|
||||
|
||||
Message body
|
||||
`
|
||||
|
||||
r := strings.NewReader(msg)
|
||||
m, err := mail.ReadMessage(r)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
header := m.Header
|
||||
fmt.Println("Date:", header.Get("Date"))
|
||||
fmt.Println("From:", header.Get("From"))
|
||||
fmt.Println("To:", header.Get("To"))
|
||||
fmt.Println("Subject:", header.Get("Subject"))
|
||||
|
||||
body, err := ioutil.ReadAll(m.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("%s", body)
|
||||
|
||||
// Output:
|
||||
// Date: Mon, 23 Jun 2015 11:40:36 -0400
|
||||
// From: Gopher <from@example.com>
|
||||
// To: Another Gopher <to@example.com>
|
||||
// Subject: Gophers at Gophercon
|
||||
// Message body
|
||||
}
|
Loading…
Reference in New Issue
Block a user