dot import blocks names in another file!

133 views
Skip to first unread message

simon place

unread,
Apr 12, 2026, 11:33:37 AM (3 days ago) Apr 12
to golang-nuts
bug? (to me should be since imports should be that file only.)



Konstantin Kulikov

unread,
Apr 12, 2026, 12:03:04 PM (3 days ago) Apr 12
to simon place, golang-nuts
Suppose this would work, which function would be called in main(), the one from another.go or the one from fmt?

simon place

unread,
Apr 12, 2026, 12:21:52 PM (3 days ago) Apr 12
to golang-nuts
i see the func in another.go is package wide so is the same as the dot import name in the main.go.

Jan Mercl

unread,
Apr 12, 2026, 4:27:21 PM (3 days ago) Apr 12
to simon place, golang-nuts
"Normal" imports (like `import "fmt"`) declare the name, used in the
package clause of package at import path "fmt", in the file scope.

Aliased imports (like `import foo "fmt"`) declare foo in the file scope.

Dot imports (your `import . "fmt"`) declare nothing in file scope, but
declare all exported names from package "fmt" in package scope, hence
the redeclared error.

Axel Wagner

unread,
Apr 13, 2026, 2:29:14 AM (2 days ago) Apr 13
to golang-nuts
It is not a bug, no. From the spec:

> No identifier may be declared twice in the same block, and no identifier may be declared in both the file and package block

The `func` declaration declares it in the package block and the dot-import declares it in the file block.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/CAA40n-WcnFJo8WNowrd58ND8cXFE%2B%2Bbd6wEpq4J064zkhY8TDQ%40mail.gmail.com.

Jan Mercl

unread,
Apr 13, 2026, 2:40:25 AM (2 days ago) Apr 13
to Axel Wagner, golang-nuts
On Mon, Apr 13, 2026 at 8:28 AM 'Axel Wagner' via golang-nuts
<golan...@googlegroups.com> wrote:

> The `func` declaration declares it in the package block and the dot-import declares it in the file block.

I thought you are wrong, but it was me: https://go.dev/play/p/PEyy4fqwf8e

I still fail to find where the specs say dot imports declare the
exported identifiers of "fmt" in file scope.

Axel Wagner

unread,
Apr 13, 2026, 2:41:32 AM (2 days ago) Apr 13
to Jan Mercl, golang-nuts

> If an explicit period (.) appears instead of a name, all the package's exported identifiers declared in that package's package block will be declared in the importing source file's file block

Jan Mercl

unread,
Apr 13, 2026, 4:53:37 AM (2 days ago) Apr 13
to Axel Wagner, golang-nuts
On Mon, Apr 13, 2026 at 8:40 AM Axel Wagner
<axel.wa...@googlemail.com> wrote:

> > If an explicit period (.) appears instead of a name, all the package's exported identifiers declared in that package's package block will be declared in the importing source file's file block

Thanks. I guess you will believe that I've read that section more than
once before posting and still managed to miss this part.

I read faster than I can read.

-j
Reply all
Reply to author
Forward
0 new messages