github.com/flyinox/gosm@v0.0.0-20171117061539-16768cb62077/src/mime/multipart/example_test.go (about) 1 // Copyright 2014 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 package multipart_test 6 7 import ( 8 "fmt" 9 "io" 10 "io/ioutil" 11 "log" 12 "mime" 13 "mime/multipart" 14 "net/mail" 15 "strings" 16 ) 17 18 func ExampleNewReader() { 19 msg := &mail.Message{ 20 Header: map[string][]string{ 21 "Content-Type": {"multipart/mixed; boundary=foo"}, 22 }, 23 Body: strings.NewReader( 24 "--foo\r\nFoo: one\r\n\r\nA section\r\n" + 25 "--foo\r\nFoo: two\r\n\r\nAnd another\r\n" + 26 "--foo--\r\n"), 27 } 28 mediaType, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type")) 29 if err != nil { 30 log.Fatal(err) 31 } 32 if strings.HasPrefix(mediaType, "multipart/") { 33 mr := multipart.NewReader(msg.Body, params["boundary"]) 34 for { 35 p, err := mr.NextPart() 36 if err == io.EOF { 37 return 38 } 39 if err != nil { 40 log.Fatal(err) 41 } 42 slurp, err := ioutil.ReadAll(p) 43 if err != nil { 44 log.Fatal(err) 45 } 46 fmt.Printf("Part %q: %q\n", p.Header.Get("Foo"), slurp) 47 } 48 } 49 50 // Output: 51 // Part "one": "A section" 52 // Part "two": "And another" 53 }