Calling Methods without their Imported package names

Are you bored with calling a package name before calling its methods every time you evoke it, like fmt.Println()?

You can just put a dot (.) in front of the package like import ( . "fmt" . "net\http" )

so you just evoke their respective methods like Println() or handleFunc() without calling the package. The Go compiler will do understand the rest

Dot imports. If a program imports a standard package using import . "path" , additional names defined in the imported package in future releases may conflict with other names defined in the program. We do not recommend the use of import . outside of tests, and using it may cause a program to fail to compile in future releases

3 Likes

Hi @jerevick83, I understand your point and your desire to make writing code more efficient.

However, there is a Go proverb that says, “Clear is better than clever.” Go is a boring language, and that’s a good thing. Writing code may become boring at times, no question. But source code is so much more often being read than written, and an easy read is much more important than saving a few keystrokes.

The explicitness of Go greatly helps with reading and understanding other people’s code.

2 Likes

This is common knowledge, yet I almost never see it used, for a reason. Perhaps the reason is that it is clearer to see fmt.Println instead of Println, an unspoken convention of assuming any method call without a package prefix is calling a method defined in the current package.

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