[root@aa test]# tree .
.
├── go.mod
├── hello1
│ ├── go.mod
│ └── hello1.go
└── main.go
1 directory, 4 files
hello1包
[root@aa gocode]# mkdir -p test/hello1 && cd test/hello1
[root@aa hello1]# go mod init hello1
go: creating new go.mod: module hello1
[root@aa hello1]# vim hello1.go
package hello1
import "fmt"
func Hello1() {
fmt.Println("this is hello1")
}
导hello1包
[root@aa hello1]# cd ..
[root@aa test]# go mod init test
go: creating new go.mod: module test
go: to add module requirements and sums:
go mod tidy
[root@aa test]# vim main.go
package main
import (
"hello1"
)
func main() {
hello1.Hello1()
}
指定hello1包的路径,默认会从goroot … 找起
[root@aa test]# go mod edit -replace hello1=./hello1
tidy 同步一下
[root@aa test]# go mod tidy
go: found hello1 in hello1 v0.0.0-00010101000000-000000000000
[root@aa test]# cat go.mod
module test
go 1.19
replace hello1 => ./hello1
require hello1 v0.0.0-00010101000000-000000000000
运行
[root@aa test]# go run main.go
this is hello1
------------end