如何定义依赖关系?

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

go: How to define dependencies?

问题

在Go语言中,依赖项是通过导入语句来指定的。在Go中,每个源文件都会声明它所依赖的包,并通过导入语句将其引入。例如:

import (
    "fmt"
    "net/http"
)

这里的fmtnet/http就是依赖的包。当你编译或运行程序时,Go编译器会自动解析并下载这些依赖项。

Go没有像Node.js的package.json那样的"元"定义文件。相反,Go使用了一个简单的依赖管理工具,称为Go Modules。使用Go Modules,你可以在项目的根目录下创建一个go.mod文件来管理依赖项。go.mod文件会记录项目所依赖的模块及其版本信息。

你可以使用go mod init命令来初始化一个新的Go模块,并自动创建一个go.mod文件。然后,你可以使用go get命令来添加依赖项,例如:

go mod init example.com/myproject
go get github.com/gin-gonic/gin@v1.7.2

这将在go.mod文件中添加github.com/gin-gonic/gin作为项目的依赖项,并指定版本为v1.7.2

希望这能帮到你!如果你有其他问题,请随时问。

英文:

How are dependencies specified in go?

For example in node.js you have package.json and you define dependencies like this: https://docs.npmjs.com/files/package.json#dependencies

Is there a "meta" definition like package.json in go?

答案1

得分: 3

不,每个源文件都会导入包,当你编译时,你需要指定主文件(入口点)。依赖图是在编译时动态构建的。循环依赖是被禁止的。

英文:

No, each source file import packages, you specify the main file (your entry point) when you compile. The dependency graph is built on the fly at compile time. Circular dependencies are forbidden.

huangapple
  • 本文由 发表于 2017年1月19日 23:47:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/41746064.html
匿名

发表评论

匿名网友

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

确定