I am new to golang and currently looking into packages and how importing them works. I have a few questions about this import. I'm reading this post (it has over 7k likes so I guess it's accurate and it's the first thing google gives me when I type golang package
).
In order to explain what I don't understand, let me first write about what the project structure is like and what the contents of all the files are.
pkg ├── mypkg | └── mypkg.go | ├── pkg1 | └── pkg1.go | └── pkg2 └── pkg2.go go.mod main.go
As you can see, I have a module with 3 packages (4 packages including the main package). The contents of all packages are listed below.
pkg1.go Content:
package pkg1 import ( "fmt" "mymod/pkg/mypkg" ) func init() { fmt.println("hello everyone from pkg1 init") } func hellofrompkg1() { fmt.println("hello from pk1") mypkg.print() }
pkg2.go Content:
package pkg2 import ( "fmt" "mymod/pkg/mypkg" ) func init() { fmt.println("hello everyone from pkg2 init") } func hellofrompkg2() { fmt.println("hello from pk2") mypkg.print() }
Contents of mypkg:
package mypkg import "fmt" func init() { fmt.println("hello everyone from mypkg init") } var prom = 10 func print() { fmt.printf("address of prom inside mypkg is: %p\n", &prom) }
main.go Content:
package main import ( "fmt" "mymod/pkg/pkg1" "mymod/pkg/pkg2" ) func init() { fmt.println("hello everyone from main init") } func main() { pkg1.hellofrompkg1() pkg2.hellofrompkg2() }
Thus, main.go
contains pkg1
and pkg2
, and pkg1
and pkg2
both containmypkg
. The article I cited states the following (in bold):
the main thing to remember is, an imported package is initialized only once per package.
With this in mind, I would expect the output of my program to be something like this:
hello everyone from mypkg init hello everyone from pkg1 init hello everyone from mypkg init hello everyone from pkg2 init hello everyone from main init hello from pk1 address of prom inside mypkg is: 0xfee360 (some address) hello from pk2 address of prom inside mypkg is: 0xf321a3 (another address)
My expectation is to follow these steps:
prom
In my case (2.1.1)prom
In my case (3.1.1)Instead, I get the following output:
Hello everyone from mypkg init Hello everyone from pkg1 init Hello everyone from pkg2 init Hello everyone from main init Hello from pk1 address of prom inside mypkg is: 0x8fe360 Hello from pk2 address of prom inside mypkg is: 0x8fe360
It looks like mypkg is only initialized once during the first import? ! Additionally, the global variable prom
has the same address in pkg1 and pkg2 (0x8fe360
in this case).
So my question is:
mypkg
is initialized once in pkg1
and another time in pkg2
. I know that for some people these questions are about some basic knowledge of golang, but for me as a beginner it caused some misunderstandings. What's more, the results of the program's work didn't match what the authors of the articles that came up first in Google searches had written. Any help is welcome. wish all the best!
The packages contained in the program are a set, which is the transitive closure of all imported packages starting from main
. Right now:
As for initialization: your steps are correct, just mypkg
only initialize once. There are not multiple copies of mypkg
in the binary.
The above is the detailed content of How does the golang package import process work? Is there a way to initialize a package multiple times in the same module?. For more information, please follow other related articles on the PHP Chinese website!