github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gofrontend/libgo/go/net/mail/example_test.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build ignore 6 7 package mail_test 8 9 import ( 10 "fmt" 11 "io/ioutil" 12 "log" 13 "net/mail" 14 "strings" 15 ) 16 17 func ExampleParseAddressList() { 18 const list = "Alice <alice@example.com>, Bob <bob@example.com>, Eve <eve@example.com>" 19 emails, err := mail.ParseAddressList(list) 20 if err != nil { 21 log.Fatal(err) 22 } 23 24 for _, v := range emails { 25 fmt.Println(v.Name, v.Address) 26 } 27 28 // Output: 29 // Alice alice@example.com 30 // Bob bob@example.com 31 // Eve eve@example.com 32 } 33 34 func ExampleParseAddress() { 35 e, err := mail.ParseAddress("Alice <alice@example.com>") 36 if err != nil { 37 log.Fatal(err) 38 } 39 40 fmt.Println(e.Name, e.Address) 41 42 // Output: 43 // Alice alice@example.com 44 } 45 46 func ExampleReadMessage() { 47 msg := `Date: Mon, 23 Jun 2015 11:40:36 -0400 48 From: Gopher <from@example.com> 49 To: Another Gopher <to@example.com> 50 Subject: Gophers at Gophercon 51 52 Message body 53 ` 54 55 r := strings.NewReader(msg) 56 m, err := mail.ReadMessage(r) 57 if err != nil { 58 log.Fatal(err) 59 } 60 61 header := m.Header 62 fmt.Println("Date:", header.Get("Date")) 63 fmt.Println("From:", header.Get("From")) 64 fmt.Println("To:", header.Get("To")) 65 fmt.Println("Subject:", header.Get("Subject")) 66 67 body, err := ioutil.ReadAll(m.Body) 68 if err != nil { 69 log.Fatal(err) 70 } 71 fmt.Printf("%s", body) 72 73 // Output: 74 // Date: Mon, 23 Jun 2015 11:40:36 -0400 75 // From: Gopher <from@example.com> 76 // To: Another Gopher <to@example.com> 77 // Subject: Gophers at Gophercon 78 // Message body 79 }