A package only works using dot imports is this normal

Hi,
I have a strange case using an package that only works by using an dot import.

If I delete the dot and run gofmt on save, it removes the import of this package so the example code using the package becomes pretty useless I’m just curious what causes this since I normally never had to use dot imports before.

Here’s the link to the example code:

If you remove the dot import then the symbol NewTM, etc must (according to the Go spec) be defined in the current package. You’ll need to rename all the symbols from the tm package to tm.NewTM etc

3 Likes

Thanks for the great answer,

I didn’t know about importing packages referred to the local file block without an qualifier. I never seen this before are there other reasons than naming the symbols not to use it?

I found it in the Go spec: https://golang.org/ref/spec#Import_declarations

1 Like

Using dot imports is frowned upon as it removes the desirable property of being able to understand where a symbol comes from by just looking at it. Do not use dot imports.

There are some very few reasonable use cases for dot imports. You’re unlikely to run into them at the start of your Go career.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.