如何在GOLANG导入本地包

  • 本文关键字:导入 GOLANG go
  • 更新时间 :
  • 英文 :


我是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:UserstonyfDesktopgo-workspace-2.0srcpackagetest

这就是我的mymath.go所在的C:UserstonyfDesktopgo-workspace-2.0srcpackagetestmymath

当我运行main.go时,我会得到这样的错误main.go:5:2: package packagetest/mymath is not in GOROOT (C:Program FilesGosrcpackagetestmymath)

问题

could not import packagetest/mymath (cannot find package "packagetest/mymath" in any of 
C:Program FilesGosrcpackagetestmymath (from $GOROOT)
Csrcpackagetestmymath (from $GOPATH)
UserstonyfDesktopgo-workspace-2.0srcpackagetestmymath (from $GOPATH))

请帮助🥺

您只需要首先运行以下命令:

go mod init <appname>

在相同的应用程序路径中。

最新更新