如何从相对本地文件夹导入 "include" 意义上的 .go 文件
How to import a .go file in the "include" sense from relative local folder
我正在尝试包含一个“.go”文件,其中包含我在不同的独立项目中需要的一些结构声明
目前报错如下:
main.go:8:2: module include@latest found (v0.0.0, replaced by ./include), but does not contain package include/declare
树结构:
/home/dev/go/sample
include/
main.go
go.mod
/home/dev/go/sample/include
declare.go
go.mod
/home/dev/sample/go的内容。mod:
go 1.14
module include
replace include v0.0.0 => ./include
/home/dev/sample/include/go的内容。mod:
module include
go 1.14
require include v0.0.0
replace include v0.0.0 => include
从“main.go”导入:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"include/declare"
)
谢谢
在@enzo 提出的刻苦阅读之后,我的请求现在可以使用了
示例结构:
├── go.mod
├── include
│ ├── sourceA.go
│ └── sourceB.go
└── myapp.go
解决方案是在主应用程序级别创建一个简单正确的“go.mod”文件。在我的例子中,不需要在“include”文件夹中有一个
go mod init <name of your app>
go.mod 文件的内容:
MyApp module
go 1.14
include/sourceA.go的内容:
package include
const ValueA = "a value from sourceA.go"
func FunctionA () {
println("Hello: sourceA.go")
}
include/sourceB.go的内容:
package include
const ValueB = "a value from sourceB.go"
func FunctionB () {
println("Hello: sourceB.go")
}
myapp.go的内容:
package main
import "myapp/include"
func main() {
include.FunctionA()
include.FunctionB()
println(include.ValueA)
println(include.ValueB)
}
导入管理(一切都是module/packages)初学Go的学徒不一定能看懂,但是一旦“理解”,因为它比在其他语言中发现的更合乎逻辑,尤其是对于“包含”方法。
希望这对其他人更好的代码文件组织有用
(肯定有更好的方法,但是这个暂时满足我的需要)
我正在尝试包含一个“.go”文件,其中包含我在不同的独立项目中需要的一些结构声明
目前报错如下:
main.go:8:2: module include@latest found (v0.0.0, replaced by ./include), but does not contain package include/declare
树结构:
/home/dev/go/sample
include/
main.go
go.mod
/home/dev/go/sample/include
declare.go
go.mod
/home/dev/sample/go的内容。mod:
go 1.14
module include
replace include v0.0.0 => ./include
/home/dev/sample/include/go的内容。mod:
module include
go 1.14
require include v0.0.0
replace include v0.0.0 => include
从“main.go”导入:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"include/declare"
)
谢谢
在@enzo 提出的刻苦阅读之后,我的请求现在可以使用了
示例结构:
├── go.mod
├── include
│ ├── sourceA.go
│ └── sourceB.go
└── myapp.go
解决方案是在主应用程序级别创建一个简单正确的“go.mod”文件。在我的例子中,不需要在“include”文件夹中有一个
go mod init <name of your app>
go.mod 文件的内容:
MyApp module
go 1.14
include/sourceA.go的内容:
package include
const ValueA = "a value from sourceA.go"
func FunctionA () {
println("Hello: sourceA.go")
}
include/sourceB.go的内容:
package include
const ValueB = "a value from sourceB.go"
func FunctionB () {
println("Hello: sourceB.go")
}
myapp.go的内容:
package main
import "myapp/include"
func main() {
include.FunctionA()
include.FunctionB()
println(include.ValueA)
println(include.ValueB)
}
导入管理(一切都是module/packages)初学Go的学徒不一定能看懂,但是一旦“理解”,因为它比在其他语言中发现的更合乎逻辑,尤其是对于“包含”方法。
希望这对其他人更好的代码文件组织有用 (肯定有更好的方法,但是这个暂时满足我的需要)