-2

Why do we need to put a package declaration in every file in folder to mark the specified .go files as a part of a package?

Instead the directory name itself could be a package name for all files it contains.

So what the ideological point for this decision?

  • 5
    The directory name is not related to the package name – JimB Apr 04 '17 at 20:01
  • 1
    As an example of what @JimB is talking about package `import "google.golang.org/api/oauth2/v2"` imports the `oauth2` package and is used as such: `oauth2.New(...)`. – RayfenWindspear Apr 04 '17 at 20:21
  • 2
    Or pretty much _any_ `package main` – JimB Apr 04 '17 at 20:23
  • I understand that it is NOT related. BUT my question was not about the current implementation. It is about the advantages of current implementation versus the old one plain directories. – Egor Shalashnikov Apr 04 '17 at 21:12
  • Package main is just as simple as putting your main proc into main folder. – Egor Shalashnikov Apr 04 '17 at 21:13
  • Anyway, I've seen the doc from Rob Pike or some related guys with the detailed description of their package ideology , with explanations and examples. If you remeber link to it, it'll be great – Egor Shalashnikov Apr 04 '17 at 21:15
  • The use of the package declaration is that you may use a Go _identifier_ as the package name. A folder name may not be a valid Go identifier, and a Go identifier may not be a valid folder name. More on this in this answer: [What is the purpose of the package declaration?](http://stackoverflow.com/questions/38563990/what-is-the-purpose-of-the-package-declaration/38564329#38564329) – icza Apr 05 '17 at 07:37

1 Answers1

1

Instead the directory name itself could be a package name for all files it contains.

Egor, you probably suppose that in one directory go-files only from one package.

You are almost correct if you exclude tests.

Tests are exception from rule "one directory - one package".

Alex Yu
  • 3,412
  • 1
  • 25
  • 38