可以有人给我简化一下 go mod init 吗?

huangapple go评论69阅读模式
英文:

Can someone please dumb down go mod init for me?

问题

我正在尝试使用Go的模块功能来理解它们的工作原理。我已经阅读了文档,甚至复制了完全相同的过程,但是在导入本地模块时出现了一些问题。文件树的结构如下:

    src
     |
      main -> main.go
     |
     pkg -> pkg.go

src文件夹下有两个文件夹,main和pkg。

我的问题是在哪里应该调用go mod init命令,以及应该如何命名它。这个问题困扰了我一段时间了。

英文:

I am trying to use the module capabilities of go to understand how they work. I have read the documentation and even replicated the exact process and for some reason the import of local modules. The file tree is like this

    src
     |
      main -> main.go
     |
     pkg -> pkg.go 

The src folder has two folders main and pkg.

My question is where should I call go mod init and how should I name it. This has been confusing me for a while now.

答案1

得分: 2

我将使用Windows路径,因为这是我使用的系统。首先,在某个地方创建一个新文件夹,例如C:\north。然后进入该目录,并输入以下内容:

go mod init north

然后创建C:\north\north.go文件:

package north

const Direction = "north"

接下来创建C:\north\north\north.go文件:

package main
import "north"

func main() {
   println(north.Direction)
}
英文:

I will use Windows paths, as that's what I use. First, make a new folder somewhere, for example C:\north. They go to that directory, and enter this:

go mod init north

Then make C:\north\north.go:

package north

const Direction = "north"

Then make C:\north\north\north.go:

package main
import "north"

func main() {
   println(north.Direction)
}

huangapple
  • 本文由 发表于 2021年5月19日 23:16:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/67606062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定