Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

docs: define go import convention #258

Merged
merged 1 commit into from
Sep 15, 2017

Conversation

surajssd
Copy link
Member

Add a developer doc that explains how to arrange imports so that
when new imports are added, unnecessary changes in import space
are avoided.

This was decided here #149 (comment) and we are following this since, wanted to make it official by putting it in docs.

@cdrage
Copy link
Collaborator

cdrage commented Sep 13, 2017

I feel as this is unnecessary. This is automatically formatted with gofmt or even plugins such as vim-go. Why add this to development.md?

@surajssd
Copy link
Member Author

surajssd commented Sep 13, 2017

@cdrage it does not separate in the form of sections, so this is necessary and we have to do it manually.

@cdrage
Copy link
Collaborator

cdrage commented Sep 13, 2017

@surajssd Could we direct users to vim plugins / ide plugins instead?

Using vim-go I simply type in :GoImport and it automatically organizes / imports.

@kadel what do you think? Organizing something like imports seems unnecessary in terms of development, do we really have to have a doc to emphasize this?

@surajssd
Copy link
Member Author

@cdrage yes golang plugins do sort the imports, but if you leave a line in between you end up forming sections in import section.

And the tool does not remove those empty lines, it just forms section within that block, so we are trying to form a convention here.

But if we agree on not putting any blank lines in imports then I am fine with it, but let's have something the tools are not good enough to do it on their own.

@cdrage
Copy link
Collaborator

cdrage commented Sep 13, 2017

@surajssd

Yeah, IMO this development.md is getting bigger and bigger and a lot of the stuff is either common sense (if you've gone through the Golang tutorials) or unnecessary such as these import functions. It has no effect on code and it still adheres to the gofmt style, so I don't see it necessary to add this doc. Sorry if I'm being blunt, just IMO.

@surajssd
Copy link
Member Author

@cdrage

So there have been lot of instances #180 (comment) #179 (comment) #173 (comment) #242 (comment)

where we had to point out in PRs saying that we are following the import conventions, because it was unnecessary diff in file even though that file had no other code change. And everytime I had to point to the old comment #149 (comment) where we decided this is how we are gonna settle on arranging imports.

@cdrage
Copy link
Collaborator

cdrage commented Sep 13, 2017

Ahhhhhhh that's why. Makes sense. You have a good point. @surajssd

PR LGTM.


### golang dependency import conventions

imports MUST be arranged in three sections, separated by empty line.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imports
separated by an empty line

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

for e.g.

```go
"fmt"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be better to unindent all of this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

thirdparty
```

for e.g.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"github.com/pkg/errors"
```

And once arranged in such sections let go tooling take care of sorting the
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once arranged, let gofmt sort the sequence of imports.

Would be a better sentence

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

```go
stdlib
kedge
thirdparty
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also add a named import section here like done in https://github.com/kedgeproject/kedge/blob/master/pkg/spec/resources.go#L19-L38, this makes the imports look a lot cleaner.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is for knowing what imports are coming from where, also even if the imports are named or not the sorting happens the same way.

And same convention k8s also follows.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use named imports only when there is a collision between multiple package names. Otherwise, it just adds confusion

Add a developer doc that explains how to arrange imports so that
when new imports are added, unnecessary changes in import space
are avoided.
@kadel kadel merged commit a9e40ef into kedgeproject:master Sep 15, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants