## 由头
在开发过程中,需要导入本地的module,然后涉及导入相关的系列问题,特此做贴,以询大佬。
## 场景
![mod.png](https://static.studygolang.com/200714/6e370fb97a65d68d5051dc529e764f3b.png)
## 构造的demo
父级目录如下:
```bash
PS G:\Data\gitProject\testmod> tree
卷 Gdl 的文件夹 PATH 列表
卷序列号为 A685-DF3D
G:.
├─main
└─package
├─god
└─pkg
PS G:\Data\gitProject\testmod>
```
### 首先测试packages模块
packages模块目录结构如下:
![image.png](https://static.studygolang.com/200714/5e40854a847a9784d263e00e48a59a4b.png)
```golang
// source file: god.go
package god
import "packages/pkg"
func init() {
}
func SayHello() {
pkg.SayHello()
}
```
```golang
// source file: mypackage.go
package pkg
import (
"fmt"
)
func SayHello() {
fmt.Println("say hello")
}
```
```golang
// source file: testpackages.go
package main
import "packages/god"
func main() {
god.SayHello()
}
```
```golang
// source file: go.mod
module packages
go 1.14
```
**执行结果如下:**
```bash
PS G:\Data\gitProject\testmod\package> go run .
say hello
PS G:\Data\gitProject\testmod\package>
```
#### 开始示范我的需求了
整个项目目录结构如下:
![image.png](https://static.studygolang.com/200714/6605d2a05fb47be95ac6afb6d85a6bbf.png)
then....
```golang
//source file: main.go
package main
import (
"fmt"
"packages/god"
)
func main() {
fmt.Println("there is main domain.")
god.SayHello()
}
```
```bsah
# source file: main/go.mod
module main
go 1.14
require packages/god v0.0.0
replace packages/god => ../package/god
```
##### Output:
```bash
PS G:\Data\gitProject\testmod\main> go run .
go: packages/god@v0.0.0: parsing ..\package\god\go.mod: open G:\Data\gitProject\testmod\package\god\go.mod: The system cannot find the file specified.
PS G:\Data\gitProject\testmod\main>
```
later, 根据上面的提示,说在G:\Data\gitProject\testmod\package\god\go.mod需要一个go.mod, 是否说通过go mod导入的module,只能管理一个package?
如他所愿,我加一个G:\Data\gitProject\testmod\package\god\go.mod, 再看一看,
output:
```bash
PS G:\Data\gitProject\testmod\main> cd ..\package\god\
PS G:\Data\gitProject\testmod\package\god> go mod init god
go: creating new go.mod: module god
PS G:\Data\gitProject\testmod\package\god> cd ..\..\main\
PS G:\Data\gitProject\testmod\main> go run .
..\package\god\god.go:3:8: package packages/pkg is not in GOROOT (G:\Applications\goCompiler\src\packages\pkg)
PS G:\Data\gitProject\testmod\main>
````
然后就回到了一个导入module外一个包的问题上,那么我直接在main中import "god", 使用replace重定位。
对main做如下修改:
![image.png](https://static.studygolang.com/200714/5d818bd4cd8739508975a2dc08c54bae.png)
对god做如下修改:
![image.png](https://static.studygolang.com/200714/0ec7f75fbd1d53ccb9e904e46015c3d2.png)
在pkg目录下执行`go mod init pkg`
然后运行main:
output:
```bash
PS G:\Data\gitProject\testmod\package\pkg> go mod init pkg
go: creating new go.mod: module pkg
PS G:\Data\gitProject\testmod\package\pkg>
PS G:\Data\gitProject\testmod\package\pkg> cd ..\..\main\
PS G:\Data\gitProject\testmod\main> go run .
go: god@v0.0.0 requires
pkg@v0.0.0: malformed module path "pkg": missing dot in first path element
PS G:\Data\gitProject\testmod\main>
```
## 我的问题如上所述,大佬们走过路过,瞟一眼,指点下
有疑问加站长微信联系(非本文作者)