报错
package xx is not in GOROOT
build command-line-arguments: cannot find module for path xx
项目结构
tips: 子文件夹和包名一致更好
FuncHouse.go
package Leetcode
import (
"math"
"fmt"
)
func A(){
fmt.Println("abc")
}
导入方法
1. 在Algorithm项目文件夹下运行命令,生成一个go.mod文件:
go mod init Algorithm
2. 在main.go
使用的绝对路径为import “Alogorithm/Leetcode"导入FuncHouse.go的函数:
package main
import (
"Algorithm/Leetcode"
"fmt"
)
func main() {
fmt.Println(Leetcode.A())
}
tips:使用包名进行调用,而不是go文件名
3. 再次运行main.go成功!