package name of the importer

44 views
Skip to first unread message

Andreas Nilsen

unread,
Jun 20, 2018, 8:53:26 AM6/20/18
to golang-nuts
Is it possible for a package to get the package name of the importer?

if pack_2 import pack_1, is it possible for pack_1 to get the name of pack_2?

Axel Wagner

unread,
Jun 20, 2018, 9:05:04 AM6/20/18
to andn...@gmail.com, golang-nuts
What exactly are you trying to do? It's possible to pass it as a string. You can also use reflect, if pack_1 gets access to a type defined in pack_2. Lastly, you can use the runtime package to get the caller of a function and use some heuristic to figure out the package path from the file path.

In general, though, no. Among other things, this isn't particularly well-defined, as a package is often imported from multiple packages in a single program and because we'd packages are the compilation unit and we want to reuse a built package for different binaries.

On Wed, Jun 20, 2018 at 2:53 PM Andreas Nilsen <andn...@gmail.com> wrote:
Is it possible for a package to get the package name of the importer?

if pack_2 import pack_1, is it possible for pack_1 to get the name of pack_2?

--
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.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages