<p dir="auto">In the <code class="notranslate">net/mail</code> package, <code class="notranslate">Address</code> is used to generate a valid RFC 5322 address. <code class="notranslate">ParseAddress</code> is used to parse a single RFC 5322 address.</p>
<p dir="auto">There is currently a bug if the address name contains parentheses.</p>
<p dir="auto">The following code fails to run:</p>
<div class="highlight highlight-source-go notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="package main

import &quot;fmt&quot;
import &quot;net/mail&quot;

func main() {

    tests := []*mail.Address{
        {Name: &quot;Robert Müller&quot;, Address: &quot;bob@example.com&quot;},
        {Name: &quot;Robert (Bob) Müller&quot;, Address: &quot;bob@example.com&quot;},
    }

    for i, in := range tests {
        fmt.Printf(&quot;\nTest %d\n&quot;, i)

        // straight encoding, will use q-encoding if needed
        fmt.Printf(&quot;In:  %s\n&quot;, in)

        // decode the q-encoding
        out, err := mail.ParseAddress(in.String())
        if err != nil {
            // oops! cannot parse our own address?
            panic(err)
        }

        // should match our earlier q-encoding
        fmt.Printf(&quot;Out: %s\n&quot;, out)
    }
}"><pre class="notranslate"><span class="pl-k">package</span> main

<span class="pl-k">import</span> <span class="pl-s">"fmt"</span>
<span class="pl-k">import</span> <span class="pl-s">"net/mail"</span>

<span class="pl-k">func</span> <span class="pl-en">main</span>() {

    <span class="pl-s1">tests</span> <span class="pl-c1">:=</span> []<span class="pl-c1">*</span>mail.<span class="pl-smi">Address</span>{
        {<span class="pl-c1">Name</span>: <span class="pl-s">"Robert Müller"</span>, <span class="pl-c1">Address</span>: <span class="pl-s">"bob@example.com"</span>},
        {<span class="pl-c1">Name</span>: <span class="pl-s">"Robert (Bob) Müller"</span>, <span class="pl-c1">Address</span>: <span class="pl-s">"bob@example.com"</span>},
    }

    <span class="pl-k">for</span> <span class="pl-s1">i</span>, <span class="pl-s1">in</span> <span class="pl-c1">:=</span> <span class="pl-k">range</span> <span class="pl-s1">tests</span> {
        <span class="pl-s1">fmt</span>.<span class="pl-en">Printf</span>(<span class="pl-s">"<span class="pl-cce">\n</span>Test %d<span class="pl-cce">\n</span>"</span>, <span class="pl-s1">i</span>)

        <span class="pl-c">// straight encoding, will use q-encoding if needed</span>
        <span class="pl-s1">fmt</span>.<span class="pl-en">Printf</span>(<span class="pl-s">"In:  %s<span class="pl-cce">\n</span>"</span>, <span class="pl-s1">in</span>)

        <span class="pl-c">// decode the q-encoding</span>
        <span class="pl-s1">out</span>, <span class="pl-s1">err</span> <span class="pl-c1">:=</span> <span class="pl-s1">mail</span>.<span class="pl-en">ParseAddress</span>(<span class="pl-s1">in</span>.<span class="pl-en">String</span>())
        <span class="pl-k">if</span> <span class="pl-s1">err</span> <span class="pl-c1">!=</span> <span class="pl-c1">nil</span> {
            <span class="pl-c">// oops! cannot parse our own address?</span>
            <span class="pl-en">panic</span>(<span class="pl-s1">err</span>)
        }

        <span class="pl-c">// should match our earlier q-encoding</span>
        <span class="pl-s1">fmt</span>.<span class="pl-en">Printf</span>(<span class="pl-s">"Out: %s<span class="pl-cce">\n</span>"</span>, <span class="pl-s1">out</span>)
    }
}</pre></div>
<p dir="auto">Expected output:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Test 0
In:  =?utf-8?q?Robert_M=C3=BCller?= &lt;bob@example.com&gt;
Out: =?utf-8?q?Robert_M=C3=BCller?= &lt;bob@example.com&gt;

Test 1
In:  =?utf-8?q?Robert_(Bob)_M=C3=BCller?= &lt;bob@example.com&gt;
Out: =?utf-8?q?Robert_(Bob)_M=C3=BCller?= &lt;bob@example.com&gt;"><pre lang="text" class="notranslate"><code class="notranslate">Test 0
In:  =?utf-8?q?Robert_M=C3=BCller?= &lt;bob@example.com&gt;
Out: =?utf-8?q?Robert_M=C3=BCller?= &lt;bob@example.com&gt;

Test 1
In:  =?utf-8?q?Robert_(Bob)_M=C3=BCller?= &lt;bob@example.com&gt;
Out: =?utf-8?q?Robert_(Bob)_M=C3=BCller?= &lt;bob@example.com&gt;
</code></pre></div>
<p dir="auto">Actual output:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Test 0
In:  =?utf-8?q?Robert_M=C3=BCller?= &lt;bob@example.com&gt;
Out: =?utf-8?q?Robert_M=C3=BCller?= &lt;bob@example.com&gt;

Test 1
In:  =?utf-8?q?Robert_(Bob)_M=C3=BCller?= &lt;bob@example.com&gt;
panic: mail: no angle-addr

goroutine 1 [running]:
main.main()"><pre lang="text" class="notranslate"><code class="notranslate">Test 0
In:  =?utf-8?q?Robert_M=C3=BCller?= &lt;bob@example.com&gt;
Out: =?utf-8?q?Robert_M=C3=BCller?= &lt;bob@example.com&gt;

Test 1
In:  =?utf-8?q?Robert_(Bob)_M=C3=BCller?= &lt;bob@example.com&gt;
panic: mail: no angle-addr

goroutine 1 [running]:
main.main()
</code></pre></div>
<p dir="auto">See it in action <a href="https://play.golang.org/p/mtDJuwLZiD" rel="nofollow">here</a>.</p>
<p dir="auto">These functions should be symmetric if I correctly understand the documentation.</p>
<p dir="auto">The following program fails the panic:</p>
<div class="highlight highlight-source-go notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="package main

import (
    &quot;fmt&quot;
    &quot;net/mail&quot;
)

func main() {
    data := &quot;\&quot;\\\x1f,\&quot;&lt;0@0&gt;&quot;
    addr, err := mail.ParseAddress(data)
    if err != nil {
        return
    }
    _, err = mail.ParseAddress(addr.String())
    if err != nil {
        fmt.Printf(&quot;failed to parse addr: %q -&gt; %q\n&quot;, data, addr)
        panic(err)
    }
}"><pre class="notranslate"><span class="pl-k">package</span> main

<span class="pl-k">import</span> (
    <span class="pl-s">"fmt"</span>
    <span class="pl-s">"net/mail"</span>
)

<span class="pl-k">func</span> <span class="pl-en">main</span>() {
    <span class="pl-s1">data</span> <span class="pl-c1">:=</span> <span class="pl-s">"<span class="pl-cce">\"</span><span class="pl-cce">\\</span><span class="pl-cce">\x1f</span>,<span class="pl-cce">\"</span>&lt;0@0&gt;"</span>
    <span class="pl-s1">addr</span>, <span class="pl-s1">err</span> <span class="pl-c1">:=</span> <span class="pl-s1">mail</span>.<span class="pl-en">ParseAddress</span>(<span class="pl-s1">data</span>)
    <span class="pl-k">if</span> <span class="pl-s1">err</span> <span class="pl-c1">!=</span> <span class="pl-c1">nil</span> {
        <span class="pl-k">return</span>
    }
    <span class="pl-s1">_</span>, <span class="pl-s1">err</span> <span class="pl-c1">=</span> <span class="pl-s1">mail</span>.<span class="pl-en">ParseAddress</span>(<span class="pl-s1">addr</span>.<span class="pl-en">String</span>())
    <span class="pl-k">if</span> <span class="pl-s1">err</span> <span class="pl-c1">!=</span> <span class="pl-c1">nil</span> {
        <span class="pl-s1">fmt</span>.<span class="pl-en">Printf</span>(<span class="pl-s">"failed to parse addr: %q -&gt; %q<span class="pl-cce">\n</span>"</span>, <span class="pl-s1">data</span>, <span class="pl-s1">addr</span>)
        <span class="pl-en">panic</span>(<span class="pl-s1">err</span>)
    }
}</pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="failed to parse addr: &quot;\&quot;\\\x1f,\&quot;&lt;0@0&gt;&quot; -&gt; &quot;=?utf-8?q?=1F,?= &lt;0@0&gt;&quot;
panic: mail: no angle-addr"><pre class="notranslate"><code class="notranslate">failed to parse addr: "\"\\\x1f,\"&lt;0@0&gt;" -&gt; "=?utf-8?q?=1F,?= &lt;0@0&gt;"
panic: mail: no angle-addr
</code></pre></div>
<p dir="auto">ParseAddress must handle result of Address.String, or else first ParseAddress must fail.</p>
<p dir="auto">go version devel +514014c Thu Jun 18 15:54:35 2015 +0200 linux/amd64</p>