Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: Go 2: Implicit package name #21479

Closed
leonklingele opened this issue Aug 16, 2017 · 16 comments
Closed

proposal: Go 2: Implicit package name #21479

leonklingele opened this issue Aug 16, 2017 · 16 comments
Labels
LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@leonklingele
Copy link
Contributor

Proposal

As the package name of a file is always the name of the directory the file resides in (except package main), why not remove that package statement it and make it implicit?

Example

$ pwd
$GOPATH/src/domain/myuser/somepackage
$ cat subpackage1/subpackage2/consts.go
package subpackage2 // Redundant, this _must_ always equal literally "subpackage2" (no quotes)

const (
    SomeConst = 42
)

This saves at least two lines in every Go file which are redundant and make refactoring code harder (because you need to adjust the package name when moving the file to a different directory == different package).

I assume this can easily be implemented in the Lexer which just dynamically generates and spits out the additional two(?) tokens.

What to do about "package main"?

Well, as http://www.monogrammedchalk.com/go-2-for-teaching/ suggests, just use the package which defines func main() { as your package main.

Is there something I'm not aware of which makes this change impossible?

@gopherbot gopherbot added this to the Proposal milestone Aug 16, 2017
@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change LanguageChange labels Aug 16, 2017
@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented Aug 16, 2017

We can't remove the package declaration entirely, because it's necessary to distinguish tests that run in package x from those that run in package x_test.

Also there are build systems for Go other than the go tool, such as Bazel, and those systems do not use the same directory layout.

Also the package declaration serves as a hook for the package comment in godoc.

I suppose that we could make the package declaration optional.

@leonklingele
Copy link
Contributor Author

leonklingele commented Aug 16, 2017

because it's necessary to distinguish tests that run in package x from those that run in package x_test

Oh, you're right. But there has to be a better solution than to mark all files with a package statement. Either way, the tests (if not in the same package as the testees) need to be marked somehow.
Some possible solutions:

  1. package x_test (which includes tests for package x). This is as it currently is. Not my favorite solution because again, it has package in it which I want to abstract away.
  2. package test, simply drop the x_ prefix, otherwise same as 1. I prefer this to 1.
  3. Introduce a new keyword to mark such test files, e.g. test (probably a bad name) that needs to be the first non-comment token — the same as package is right now
  4. Something else?

EDIT:
Fix typo.

@leonklingele
Copy link
Contributor Author

Also there are build systems for Go other than the go tool, such as Bazel, and those systems do not use the same directory layout.

I have never used such a system and I guess a lot of others haven't as well.
Why can't they adapt? I'm asking this to be a feature of Go 2, which might well break existing stuff.
Making the package name optional would work, those who want to use such a build system need to "annotate" their files as they already do right now.

Also the package declaration serves as a hook for the package common in godoc.

What do you mean by that? Why can't godoc adapt?

@davecheney
Copy link
Contributor

Leon, what do you want to accomplish with this proposal? What are the problems, in your experience, you have encountered with the package declaration? It may be possible to address your issues in another way.

@leonklingele
Copy link
Contributor Author

@davecheney

This saves at least two lines in every Go file which are redundant and make refactoring code harder

Why should anyone bother with package names? It's something you do not need, or do you?
Sure, my editor already auto-inserts an appropriate package name for new files, but what's the purpose of something which can be done automatically and implicitly?

@davecheney
Copy link
Contributor

Leon, this is a justification for your proposal, not the problem it solves. There are cases where the packages decl cannot be inferred automatically for instance, when the compiler is invoked directly without going through the Go tool.

@ianlancetaylor
Copy link
Contributor

The godoc issue is simply that we want to have a way to provide a comment that describes a package. You see these comments on https://godoc.org, for example. Right now godoc displays the comment adjacent to the package declaration (with the expectation that there is only one such comment in a multi-file package). If there is no package declaration, godoc needs some other mechanism for designating the package comment.

A separate build system like bazel can't adapt because they permit multiple Go packages per directory, and because they permit files to be generated at build time.

@leonklingele
Copy link
Contributor Author

@davecheney

this is a justification for your proposal, not the problem it solves

it makes refactoring harder, it introduces "complexity" (ok, maybe I'm exaggerating a bit) where complexity is not required. Isn't that enough? Something which is not required necessarily shouldn't be required strictly.

There are cases where the packages decl cannot be inferred automatically for instance

Then simply do add the package name to your files if you need them. Why should one be required to use them?

when the compiler is invoked directly without going through the Go tool

When is this the case? Don't you consider this to be edge-cases? Again, simply include them if you like to.

@coelho
Copy link

coelho commented Aug 16, 2017

I've personally seen issues as well where a package declaration accidentally wasn't the correct package name (differed from the folder name) and nobody noticed because Go just accepts whatever is typed there. Was only an issue from the standpoint that it looked awkward and inconsistent. It really makes absolutely no difference and may as well be omitted in most cases.

@cznic
Copy link
Contributor

cznic commented Aug 17, 2017

As the package name of a file is always the name of the directory the file resides in (except package main), ...

The above was never true: s/always/most often/

@metakeule
Copy link

metakeule commented Aug 30, 2017

You could make the package name optional when it matches the directory name. That should cover all cases and behave exactly as today. Parsers not having access to the path should get the name passed in.

I am in favor of the proposal because it makes refactoring easier in early stages (=pre release) of development (e.g. split out files to sub packages etc).

@bronze1man
Copy link
Contributor

bronze1man commented Oct 18, 2017

I think We can remove the package declaration entirely in go2.0
We also can make the package declaration optional in go1.10

  • You can define main() function to mark as program entry point.
  • You can use build flag to mark this file is belong to test.
  • If you want to test the code in same directory in another package to workaround import circle, you can just move your test code to new directory.Thousands of directories and files is not a problem for today computer and os.

The package declaration is totally useless for me.
I think I should write a tool to auto correct and generate that line.
So that I can move and reflect code easier.

@andlabs
Copy link
Contributor

andlabs commented Jan 31, 2018

What refactoring is made easier by dropping package declarations?

Ian has repeatedly told you one thing that invokes the Go compiler directly: the Bazel build system. Considering who makes Bazel, it is probably unwise to expect them to change the philosophies they have decided on.

@bronze1man
Copy link
Contributor

bronze1man commented Feb 1, 2018

What refactoring is made easier by dropping package declarations?

Changing the last part of the package name become easier when the package declaration is deleted.

it is probably unwise to expect them to change the philosophies they have decided on.

I think the philosophies is made to solve problem to help developer, not make more problem.

@ianlancetaylor
Copy link
Contributor

I think there are enough reasons here to show that the package clause can not simply be eliminated in Go 2 without requiring various related changes. The benefits of eliminating the package clause seem small.

We could make the package clause optional under certain conditions, but that seems to make things more complicated. People will start writing style guides about when the package clause should exist, or not. Files may sometimes change behavior when they change directories.

The additional complexity seems not worth the benefits.

@UFOXD
Copy link

UFOXD commented Dec 28, 2018

Just remove the "package "

@golang golang locked as resolved and limited conversation to collaborators Dec 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

10 participants