go - 如何在 GOLANG 中导入本地包?
问题描述
我是 GO(和程序编程)的新手,我正在编写示例代码
我的 main.go 导入语句是
package main
import (
"fmt"
"packagetest/mymath"
)
func main() {
fmt.Println(mymath.Add(2, 3))
}
我的 mymath.go 包是
package mymath
func Add(a, b int) int {
return a + b
}
func sub(a, b int) int {
return a - b
}
我的 GOPATH 是 C:\Users\tonyf\Desktop\go-workspace-2.0
这是我的 main.go 所在的位置C:\Users\tonyf\Desktop\go-workspace-2.0\src\packagetest
这就是我的 mymath.go 所在的位置C:\Users\tonyf\Desktop\go-workspace-2.0\src\packagetest\mymath
当我运行 main.go 时,出现这样的错误
main.go:5:2: package packagetest/mymath is not in GOROOT (C:\Program Files\Go\src\packagetest\mymath)
和问题
could not import packagetest/mymath (cannot find package "packagetest/mymath" in any of
C:\Program Files\Go\src\packagetest\mymath (from $GOROOT)
C\src\packagetest\mymath (from $GOPATH)
\Users\tonyf\Desktop\go-workspace-2.0\src\packagetest\mymath (from $GOPATH))
请帮忙
解决方案
你只需要先运行这个命令:
go mod init <appname>
在同一应用程序路径中。
推荐阅读
- apache-kafka - 卡夫卡 - 故障排除 .NotEnoughReplicasException
- mongodb - 如何在 Atlas MongoDB 上实现不到一小时的自动备份?
- c# - Visual Studio Mac 的文件权限异常
- c++ - C++中的继承,如何从类中调用特定函数
- c# - 有什么比这更好的在 if 语句中交换变量的方法吗?
- mongodb - MongoDB 更新数组中错误的子文档
- python - 注释图形时在python终端中将文本打印为粗体和下划线:RuntimeWarning
- sql-server - Azure Elastic Pool Database SQL Connections are Timing Out - DTU Purchasing model
- xml - Is the UWP manifest element "uap6:SpatialBoundingBox" functioning for the HoloLens 2?
- angular - Angular: API request based on Input changes