1、新建本地模块,在任意目录下新建目录algmod
在algmod目录下执行 go mod init example.com/forest506/algmod,其中example.com/forest506/algmod是假的,不存在url,这里可以看成就是一个名称
在algmod目录下新建文件alg.go,代码如下:
package alg
import "fmt"
func Add(a int, b int) string {
return fmt.Sprintf("alg: %d and %d is %d", a, b, a+b)
}
2、在algmod同级目录下,新建usemodule目录,在usemodule目录下执行go mod init test;
新建文件main.go,内容如下:
package main
import (
"fmt"
"example.com/forest506/algmod"
)
func main() {
fmt.Println(alg.Add(10, 12))
}
3、修改usemodule目录下go.mod,改为如下内容:
module mod
go 1.13
require example.com/forest506/algmod v0.0.0
replace example.com/forest506/algmod => ../algmod
直接编译运行即可