Skip to content

Commit

Permalink
Import Aliasing: Use if basename doesn't match
Browse files Browse the repository at this point in the history
This adds a note to the import aliasing section that import aliasing
must be used if the base name doesn't match the package name.

This is placed before the more general recommendation of avoiding import
aliasing otherwise because it's more important to use named imports when
the basename doesn't match.
  • Loading branch information
abhinav committed Aug 13, 2019
1 parent c1c23fe commit 9aaee08
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions style.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ sPtrs[1].Write("test")
```

Similarly, an interface can be satisfied by a pointer, even if the method has a
value receiver. Example from the Go at Uber presentation:
value receiver.

```go
type F interface {
Expand Down Expand Up @@ -1168,8 +1168,20 @@ for the purpose of grouping related test cases, e.g.,

### Import Aliasing

Import aliases should be avoided unless there is a direct conflict between
imports.
Import aliasing must be used if the package name does not match the last
element of the import path.

```go
import (
"net/http"

client "example.com/client-go"
trace "example.com/trace/v2"
)
```

In all other scenarios, import aliases should be avoided unless there is a
direct conflict between imports.

<table>
<thead><tr><th>Bad</th><th>Good</th></tr></thead>
Expand Down Expand Up @@ -1201,9 +1213,6 @@ import (
</td></tr>
</tbody></table>

<!-- TODO: named imports should always be used if basename does not match
package name. -->

### Function Grouping and Ordering

- Functions should be sorted in rough call order.
Expand Down

0 comments on commit 9aaee08

Please sign in to comment.