我正在尝试在 Go 中制作一个简单的计算器。我正在以这样一种方式设计它,即我可以先构建一个命令行界面并轻松地在 GUI 界面中交换。项目位置是$GOPATH/src/gocalc
(以下所有路径均相对于项目位置)。命令行界面逻辑存储在一个文件中gocalc.go
。计算器逻辑存储在文件calcfns/calcfns.go
和operations/operations.go
. 所有文件的包名与其文件名(无扩展名)相同,但主程序 ,gocalc.go
位于package main
calcfns.go
operations.go
通过进口import "gocalc/operations"
; 通过gocalc.go
进口calcfns.go
import "gocalc/calcfns"
总结一下:
$GOPATH/src/gocalc/
gocalc.go
package main
import "gocalc/calcfns"
calcfns/
calcfns.go
package calcfns
import "gocalc/operations"
operations/
operations.go
package operations
当我尝试go build operations
(从项目目录)时,我得到响应:can't load package: package operations: import "operations": cannot find package
当我尝试go build gocalc/operations
时,我得到can't load package: package gocalc/operations: import "gocalc/operations": cannot find package
当我尝试go build operations/operations.go
时,它编译得很好
当我尝试go build calcfns
orgo build gocalc/calcfns
时,我收到can't load package...
消息,类似于操作中的消息;但是,当我尝试构建calcfns/calcfns.go
它时,import 语句会阻塞:import "gocalc/operations": cannot find package
最后,当我go build .
从项目目录尝试时,它与上一段类似:import "gocalc/calcfns": cannot find package
go build
我应该如何以不会失败的方式构建我的子包和/或导入语句?