搜索
首页后端开发Golang`go build` 和 `go install` 之间有什么区别,它们在哪里安装可执行文件?

What's the Difference Between `go build` and `go install`, and Where Do They Install Executables?

剖析“go install”:揭秘构建和安装过程

文档中经常没有解释 go build 和 go 之间的区别安装可能会让开发人员感到困惑。虽然大多数人期望 go install 通过将编译的可执行文件重新定位到指定位置来镜像 make install 的功能,但他们惊讶地发现结果驻留在 GOROOT/bin 目录中。本文旨在阐明这两个命令的目的和行为。

实际发生了什么?

go build 是一个简单的编译工具。它只是组装可执行文件并将其存储在指定的目的地。另一方面,go install 执行稍微复杂的过程:

  • 它编译可执行文件,就像 go build 一样。
  • 它将可执行文件移动到 $GOPATH/bin ,确保可以从命令行访问它。
  • 它将程序导入的所有非主包缓存在 $GOPATH/pkg 中。如果源代码未更改,则在后续编译期间会利用此缓存。

可视化包树

为了说明 go build 和go install,考虑以下包树:

.
├── bin
│   └── hello  # by go install
└── src   
    └── hello
        ├── hello  # by go build
        └── hello.go

hello 可执行文件由 go build 编译并驻留在src/hello 目录,而 go install 则放在 $GOPATH/bin 中,并将依赖缓存存储在 $GOPATH/pkg 中。

安装位置可以自定义吗?

与make install不同,go install不提供指定自定义安装目录的选项。为了实现这种级别的控制,有必要创建一个定义构建和安装过程的 Makefile。但是,通常不建议使用这种方法。

其他上下文

有关更多见解,请参阅有关以下主题的官方文档:

  • [走构建](https://go.dev/ref/cmd/go#hdr-Compile_packages_and_dependency)
  • [去安装](https://go.dev/ref/cmd/go#hdr-Install_and_build_packages_and_dependency)

以上是`go build` 和 `go install` 之间有什么区别,它们在哪里安装可执行文件?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
测试代码依赖于INET功能的代码测试代码依赖于INET功能的代码May 03, 2025 am 12:20 AM

whentestinggocodewithinitfunctions,useexplicitseTupfunctionsorseParateTestFileSteSteTepteTementDippedDependendendencyOnInItfunctionsIdeFunctionSideFunctionsEffect.1)useexplicitsetupfunctionStocontrolglobalvaribalization.2)createSepEpontrolglobalvarialization

将GO的错误处理方法与其他语言进行比较将GO的错误处理方法与其他语言进行比较May 03, 2025 am 12:20 AM

go'serrorhandlingurturnserrorsasvalues,与Javaandpythonwhichuseexceptions.1)go'smethodensursexplitirorhanderling,propertingrobustcodebutincreasingverbosity.2)

设计有效界面的最佳实践设计有效界面的最佳实践May 03, 2025 am 12:18 AM

AnefactiveInterfaceoisminimal,clear and promotesloosecoupling.1)minimizeTheInterfaceForflexibility andeaseofimplementation.2)useInterInterfaceForeabStractionTosWapImplementations withCallingCallingCode.3)

集中式错误处理策略集中式错误处理策略May 03, 2025 am 12:17 AM

集中式错误处理在Go语言中可以提升代码的可读性和可维护性。其实现方式和优势包括:1.将错误处理逻辑从业务逻辑中分离,简化代码。2.通过集中处理错误,确保错误处理的一致性。3.使用defer和recover来捕获和处理panic,增强程序健壮性。

init in Init函数的替代方案,用于go中的包装初始化init in Init函数的替代方案,用于go中的包装初始化May 03, 2025 am 12:17 AM

Ingo,替代词Inivuntionsionializatializatializationfunctionsandsingletons.1)customInitializationfunctions hallowexpliticpliticpliticconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconcontirization curssementializatizatupsetups.2)单次固定元素限制ininconinconcurrent

与GO接口键入断言和类型开关与GO接口键入断言和类型开关May 02, 2025 am 12:20 AM

Gohandlesinterfacesandtypeassertionseffectively,enhancingcodeflexibilityandrobustness.1)Typeassertionsallowruntimetypechecking,asseenwiththeShapeinterfaceandCircletype.2)Typeswitcheshandlemultipletypesefficiently,usefulforvariousshapesimplementingthe

使用errors.is和错误。使用errors.is和错误。May 02, 2025 am 12:11 AM

Go语言的错误处理通过errors.Is和errors.As函数变得更加灵活和可读。1.errors.Is用于检查错误是否与指定错误相同,适用于错误链的处理。2.errors.As不仅能检查错误类型,还能将错误转换为具体类型,方便提取错误信息。使用这些函数可以简化错误处理逻辑,但需注意错误链的正确传递和避免过度依赖以防代码复杂化。

在GO中进行性能调整:优化您的应用程序在GO中进行性能调整:优化您的应用程序May 02, 2025 am 12:06 AM

tomakegoapplicationsRunfasterandMorefly,useProflingTools,leverageConCurrency,andManageMoryfectily.1)usepprofforcpuorforcpuandmemoryproflingtoidentifybottlenecks.2)upitizegorizegoroutizegoroutinesandchannelstoparalletaparelalyizetasksandimproverperformance.3)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境