<p dir="auto">Please answer these questions before submitting your issue. Thanks!</p>
<p dir="auto">I believe goimports does not automatically include package unsafe even if referenced in the source, which is fine, but when package unsafe is imported it can confuse goimports and produces syntactically correct, but unexpected output, such as:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import (
    &quot;reflect&quot;
    &quot;unsafe&quot;
)
import &quot;fmt&quot;"><pre class="notranslate"><code class="notranslate">import (
    "reflect"
    "unsafe"
)
import "fmt"
</code></pre></div>
<ol dir="auto">
<li>What version of Go are you using (<code class="notranslate">go version</code>)?</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="go version go1.6 linux/amd64"><pre class="notranslate"><code class="notranslate">go version go1.6 linux/amd64
</code></pre></div>
<ol dir="auto">
<li>What operating system and processor architecture are you using (<code class="notranslate">go env</code>)?</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="GOARCH=&quot;amd64&quot;
GOBIN=&quot;&quot;
GOEXE=&quot;&quot;
GOHOSTARCH=&quot;amd64&quot;
GOHOSTOS=&quot;linux&quot;
GOOS=&quot;linux&quot;
GORACE=&quot;&quot;
GOROOT=&quot;/usr/lib/google-golang&quot;
GOTOOLDIR=&quot;/usr/lib/google-golang/pkg/tool/linux_amd64&quot;
GO15VENDOREXPERIMENT=&quot;1&quot;
CC=&quot;gcc&quot;
GOGCCFLAGS=&quot;-fPIC -m64 -pthread -fmessage-length=0 -gno-record-gcc-switches -fdebug-prefix-map=/tmp/go-build262021310=/tmp/go-build&quot;
CXX=&quot;g++&quot;
CGO_ENABLED=&quot;1&quot;"><pre class="notranslate"><code class="notranslate">GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GORACE=""
GOROOT="/usr/lib/google-golang"
GOTOOLDIR="/usr/lib/google-golang/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -gno-record-gcc-switches -fdebug-prefix-map=/tmp/go-build262021310=/tmp/go-build"
CXX="g++"
CGO_ENABLED="1"
</code></pre></div>
<ol dir="auto">
<li>What did you do?</li>
</ol>
<p dir="auto">Ran goimports on the source code <a href="http://play.golang.org/p/Ppc325UINU" rel="nofollow">http://play.golang.org/p/Ppc325UINU</a></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="package code

import &quot;unsafe&quot;
import &quot;fmt&quot;

var _ = fmt.Print
var _ = unsafe.Sizeof
var _ = reflect.Value"><pre class="notranslate"><code class="notranslate">package code

import "unsafe"
import "fmt"

var _ = fmt.Print
var _ = unsafe.Sizeof
var _ = reflect.Value
</code></pre></div>
<ol dir="auto">
<li>What did you expect to see?</li>
</ol>
<p dir="auto"><a href="http://play.golang.org/p/ySR9QrdVlp" rel="nofollow">http://play.golang.org/p/ySR9QrdVlp</a></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="package code

import (
    &quot;fmt&quot;
    &quot;reflect&quot;
    &quot;unsafe&quot;
)

var _ = fmt.Print
var _ = unsafe.Sizeof
var _ = reflect.Value"><pre class="notranslate"><code class="notranslate">package code

import (
    "fmt"
    "reflect"
    "unsafe"
)

var _ = fmt.Print
var _ = unsafe.Sizeof
var _ = reflect.Value
</code></pre></div>
<ol dir="auto">
<li>What did you see instead?</li>
</ol>
<p dir="auto"><a href="http://play.golang.org/p/i49dfImPxA" rel="nofollow">http://play.golang.org/p/i49dfImPxA</a></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="package code

import (
    &quot;reflect&quot;
    &quot;unsafe&quot;
)
import &quot;fmt&quot;

var _ = fmt.Print
var _ = unsafe.Sizeof
var _ = reflect.Value"><pre class="notranslate"><code class="notranslate">package code

import (
    "reflect"
    "unsafe"
)
import "fmt"

var _ = fmt.Print
var _ = unsafe.Sizeof
var _ = reflect.Value
</code></pre></div>
<p dir="auto">with imports like:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import &quot;bytes&quot;
import &quot;encoding/binary&quot;"><pre class="notranslate"><code class="notranslate">import "bytes"
import "encoding/binary"
</code></pre></div>
<p dir="auto">and code that adds a dependency on another package, like bufio:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="bufio.NewReader(os.Stdin)"><pre class="notranslate"><code class="notranslate">bufio.NewReader(os.Stdin)
</code></pre></div>
<p dir="auto">goimports will rewrite the imports to look like:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import (
    &quot;bufio&quot;
    &quot;bytes&quot;
)
import &quot;encoding/binary&quot;"><pre class="notranslate"><code class="notranslate">import (
    "bufio"
    "bytes"
)
import "encoding/binary"
</code></pre></div>
<p dir="auto">The formatting here is odd. I expected it to look like either:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import &quot;bufio&quot;
import &quot;bytes&quot;
import &quot;encoding/binary&quot;"><pre class="notranslate"><code class="notranslate">import "bufio"
import "bytes"
import "encoding/binary"
</code></pre></div>
<p dir="auto">where the established convention in the code of using <code class="notranslate">import</code> before every import is left alone or</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import (
    &quot;bufio&quot;
    &quot;bytes&quot;
    &quot;encoding/binary&quot;
)"><pre class="notranslate"><code class="notranslate">import (
    "bufio"
    "bytes"
    "encoding/binary"
)
</code></pre></div>
<p dir="auto">where the convention of using grouped imports is properly done.</p>