为什么我在克隆代码库后不需要执行 Go 版本的 ‘npm install’ 命令?

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

Why don't I have to do the Go equivalent of 'npm install' when I clone the repository down?

问题

在Go语言中,通常不需要像在npm中那样运行npm install命令来安装依赖项。Go使用模块管理器来管理依赖项,其中最常用的是Go Modules。当你克隆一个Go仓库到本地机器时,你可以使用go mod download命令来下载所需的依赖项。这个命令会自动检测并下载项目所需的所有依赖项。你也可以使用go mod tidy命令来自动清理和更新依赖项。希望这可以帮助你理解Go语言中的依赖项管理。

英文:

Beginner to Go but I am used to running npm install once I clone a repo to my local machine. But it does not seem like you do this in Go. Could someone help me understand? Thanks!

答案1

得分: 1

JavaScript是解释执行的。你的代码使用的各种库必须在运行时可用,并且在JavaScript程序执行时会动态加载。

Go是编译执行的。库通常在编译时解析,并且生成的二进制文件是由库和源代码的必需组件构建而成。

我说通常是因为如果启用了CGo,某些共享对象文件可能需要在运行时执行程序。在这种情况下,共享对象文件必须在系统上以某种方式安装,然后才能运行你的代码。不过,解析共享对象文件的链接是一种古老而成熟的技术,npm无法直接依赖它,所以它需要使用npm install来实现解释执行的等效功能。

如果禁用了CGo,Go二进制文件可以在安装它的架构和操作系统上执行。所有依赖项仅在编译时相关。Go模块负责在编译之前包含所需的库。

欢迎来到Go的世界。在学习过程中保持开放的心态;静态类型和缺乏继承使得Go与JavaScript非常不同,但正是其中的许多特性。所以接受Go的本质,我相信你会玩得开心并学到很多。

英文:

Javascript is interpreted. The various libraries used by your code must be available at runtime, and will be loaded in dynamically when your javascript program executes.

Go is compiled. Libraries are typically resolved at compile time, and the resultant binary has been built from the required components of libraries and your source code.

I say typically because if you have CGo enabled, some share object files may be required to execute your program at runtime. In this case, the shared object files will have to be installed somehow on a system before it can run your code. Still, resolving links to shared object files is an old and mature technology, one which npm could not directly rely on, so it needed npm install for the interpreted equivalent.

If CGo is disabled, a go binary can execute on the architecture and OS it was installed on. All dependencies are relevant at compile time only. Go modules take care of including the required libraries before compiling.

Welcome to Go. Keep an open mind as you learn; static typing and lack of inheritance makes Go very different from Javascript, but therein lies many of its features as well. So embrace Go for what it is, and I'm sure you'll have fun and learn a lot.

huangapple
  • 本文由 发表于 2022年9月16日 09:11:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/73738858.html
匿名

发表评论

匿名网友

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

确定