前往 'mod init' 创建新文件夹?路径的意义是什么?
Go 'mod init' creating new folders? what is the significance of path?
短短3天的Go语言经验。希望一个例子能更容易理解我的困惑。
root@freebsd1:/usr/home/arun/go-start/src/test2 # go mod init f1/f2/f3/f4/f5/hello
go: creating new go.mod: module f1/f2/f3/f4/f5/hello
root@freebsd1:/usr/home/arun/go-start/src/test2 #
在上面的示例中,go mod init
正在创建所有这些文件夹(f1/f2/f3/f4/f5/hello
)?。我搜索了很多,在系统中找不到任何这样的文件夹。 那这条路有什么意义呢
即使下面的命令不会 运行 如果没有按原样提及此路径
# go install f1/f2/f3/f4/f5/hello
--:编辑:--
这可能会对以后的人有所帮助(只需逐步完成这些步骤即可正确理解这一点,尤其是对于新手)
我打算制作一个程序'calculator',稍后会在GitHub上传。
我会把函数放在不同的包里,比如sum
,multiply
等等
第一步#go mod init github.com/go-arun/calculator
(这里不要混淆,这只是一个假设,将来我可能会在github中为这个项目创建一个存储库)
创建文件夹sum
(包文件夹之一,并在sum.go
里面创建)
按系统查看:
1.
root@debian1:/home/arun/lab# go mod init github.com/go-arun/calculator
go: creating new go.mod: module github.com/go-arun/calculator
root@debian1:/home/arun/lab# cat go.mod
module github.com/go-arun/calculator
go 1.15
2.
root@debian1:/home/arun/lab# cat sum/sum.go
package sum
import "fmt"
func Sum(num1,num2 int)(){
fmt.Println(num1+num2)
}
3.
root@debian1:/home/arun/lab# cat main.go
package main
import(
"github.com/go-arun/calculator/sum"
)
func main(){
n1 := 10
n2 := 10
sum.Sum(n1,n2)
}
4.
root@debian1:/home/arun/lab# tree
.
|-- go.mod
|-- main.go
`-- sum
`-- sum.go
go mod init
不会创建这些文件夹。您将 "module path" 传递给 go mod init
,它记录在它创建的 go.mod
文件中。
其中"module path"是模块根对应的导入路径前缀。模块路径和模块根目录的相对路径共同组成完整的导入路径,在一个应用中必须是唯一的。
因此,例如,如果您的模块包含一个名为 foo
的文件夹(以及其中的一个包 foo
),则它是通过路径 modulepath/foo
导入的。在您的情况下,它将是 f1/f2/f3/f4/f5/hello/foo
.
允许 moduleA
包含 foo
包,moduleB
也允许包含 foo
包。使用/导入时,首先会像 moduleA/foo
一样导入,后者会像 moduleB/foo
一样导入,因此您要导入哪个是明确的。模块路径就像一个命名空间。
建议使用与您计划或将发布模块的存储库相对应的模块路径,这样当您这样做时,go get
将能够自动获取、构建和安装您的模块。例如你可以选择一个模块路径 github.com/bob/hello
,这样当你发布你的模块时,每个人都可以通过在他们的应用程序中使用 import "github.com/bob/hello"
来获取它。
另请注意,您无需先将代码发布到远程存储库即可构建它。但仍然建议遵循此模式,这样如果您决定发布它,您将来的工作量就会减少。这里没有什么可失去的。
文档中的更多内容:Command go: Defining a module
短短3天的Go语言经验。希望一个例子能更容易理解我的困惑。
root@freebsd1:/usr/home/arun/go-start/src/test2 # go mod init f1/f2/f3/f4/f5/hello
go: creating new go.mod: module f1/f2/f3/f4/f5/hello
root@freebsd1:/usr/home/arun/go-start/src/test2 #
在上面的示例中,go mod init
正在创建所有这些文件夹(f1/f2/f3/f4/f5/hello
)?。我搜索了很多,在系统中找不到任何这样的文件夹。 那这条路有什么意义呢
即使下面的命令不会 运行 如果没有按原样提及此路径
# go install f1/f2/f3/f4/f5/hello
--:编辑:--
这可能会对以后的人有所帮助(只需逐步完成这些步骤即可正确理解这一点,尤其是对于新手)
我打算制作一个程序'calculator',稍后会在GitHub上传。
我会把函数放在不同的包里,比如
sum
,multiply
等等第一步
#go mod init github.com/go-arun/calculator
(这里不要混淆,这只是一个假设,将来我可能会在github中为这个项目创建一个存储库)创建文件夹
sum
(包文件夹之一,并在sum.go
里面创建)
按系统查看:
1.
root@debian1:/home/arun/lab# go mod init github.com/go-arun/calculator
go: creating new go.mod: module github.com/go-arun/calculator
root@debian1:/home/arun/lab# cat go.mod
module github.com/go-arun/calculator
go 1.15
2.
root@debian1:/home/arun/lab# cat sum/sum.go
package sum
import "fmt"
func Sum(num1,num2 int)(){
fmt.Println(num1+num2)
}
3.
root@debian1:/home/arun/lab# cat main.go
package main
import(
"github.com/go-arun/calculator/sum"
)
func main(){
n1 := 10
n2 := 10
sum.Sum(n1,n2)
}
4.
root@debian1:/home/arun/lab# tree
.
|-- go.mod
|-- main.go
`-- sum
`-- sum.go
go mod init
不会创建这些文件夹。您将 "module path" 传递给 go mod init
,它记录在它创建的 go.mod
文件中。
其中"module path"是模块根对应的导入路径前缀。模块路径和模块根目录的相对路径共同组成完整的导入路径,在一个应用中必须是唯一的。
因此,例如,如果您的模块包含一个名为 foo
的文件夹(以及其中的一个包 foo
),则它是通过路径 modulepath/foo
导入的。在您的情况下,它将是 f1/f2/f3/f4/f5/hello/foo
.
允许 moduleA
包含 foo
包,moduleB
也允许包含 foo
包。使用/导入时,首先会像 moduleA/foo
一样导入,后者会像 moduleB/foo
一样导入,因此您要导入哪个是明确的。模块路径就像一个命名空间。
建议使用与您计划或将发布模块的存储库相对应的模块路径,这样当您这样做时,go get
将能够自动获取、构建和安装您的模块。例如你可以选择一个模块路径 github.com/bob/hello
,这样当你发布你的模块时,每个人都可以通过在他们的应用程序中使用 import "github.com/bob/hello"
来获取它。
另请注意,您无需先将代码发布到远程存储库即可构建它。但仍然建议遵循此模式,这样如果您决定发布它,您将来的工作量就会减少。这里没有什么可失去的。
文档中的更多内容:Command go: Defining a module