I have two modules, ModuleA and ModuleB, each in a separate folder. The main.go in ModuleA needs to import a package from ModuleB. Inside the init function of the package in ModuleB, it will call “ioutil.ReadDir” and print all the files in the current directory.
My expectation is when importing ModuleB in ModuleA, it should print all files under the folder of ModuleB. However, it actually prints the files under the folder of ModuleA. May I know how to fix it?
go.mod under ModuleA
module ModuleA
go 1.19
replace ModuleB => ../ModuleB
require ModuleB v0.0.0-00010101000000-000000000000
main.go under ModuleA
package main
import "ModuleB"
func main() {
math.Add(1, 2)
}
math.go in ModuleB
package math
import (
"fmt"
"io/ioutil"
"log"
)
func init() {
fmt.Println("init moduleB math")
// ReadDir here
files, err := ioutil.ReadDir(".")
if err != nil {
log.Fatal(err)
}
for _, file := range files {
fmt.Println(file.Name(), file.IsDir())
}
}
func Add(a, b int) {
fmt.Println(a + b)
}
folder structure