搜索
首页后端开发GolangMakefile `go version` 和 `read` 命令

Makefile `go version` 和 `read` 命令

php小编百草在这篇文章中,将为大家介绍Makefile中的两个常用命令:`go version`和`read`。在编写Makefile时,这两个命令可以帮助我们获取并显示Go语言的版本信息,以及从用户输入中获取数据。通过了解和灵活运用这两个命令,我们可以更加高效地管理和构建我们的Go项目。下面就让我们一起来深入了解吧!

问题内容

我想提取并验证Makefile中的go版本。

这在 shell 中有效:

% go version | read _ _ version _ && echo "A $version Z"
A go1.21.1 Z

但在 Makefile 中不起作用

check-golang-version:
    go version | read _ _ version _ && echo "A $$version Z"

结果:

% make check-golang-version
go version | read _ _ version _ && echo "A $version Z"
A  Z

最终我想要这样的检查:

check-golang-version:
    go version | read _ _ version _ && test "$$version" = "go1.21.1" || $(error "wrong go version: $$version")

解决方法

默认情况下,make 使用 /bin/sh 作为 shell(参见 5.3.2 选择外壳)。

而且很可能当你在 shell 中执行命令时,shell 是 zshzsh 管道行为与大多数其他 shell 不同。请参阅 https://riptutorial.com/zsh/example/19869/pipes-and -subshel​​ls 为例。

我建议使用 go env GOVERSION 来获取 go 的版本并将其分配给 Makefile 变量:

version = $(shell go env GOVERSION)

check-golang-version:
ifneq ($(version), go1.21.1)
    $(error wrong go version: $(version))
endif

以上是Makefile `go version` 和 `read` 命令的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:stackoverflow。如有侵权,请联系admin@php.cn删除
掌握GO弦:深入研究'字符串”包装掌握GO弦:深入研究'字符串”包装May 12, 2025 am 12:05 AM

你应该关心Go语言中的"strings"包,因为它提供了处理文本数据的工具,从基本的字符串拼接到高级的正则表达式匹配。1)"strings"包提供了高效的字符串操作,如Join函数用于拼接字符串,避免性能问题。2)它包含高级功能,如ContainsAny函数,用于检查字符串是否包含特定字符集。3)Replace函数用于替换字符串中的子串,需注意替换顺序和大小写敏感性。4)Split函数可以根据分隔符拆分字符串,常用于正则表达式处理。5)使用时需考虑性能,如

GO中的'编码/二进制”软件包:您的二进制操作首选GO中的'编码/二进制”软件包:您的二进制操作首选May 12, 2025 am 12:03 AM

“编码/二进制”软件包interingoisentialForHandlingBinaryData,oferingToolSforreDingingAndWritingBinaryDataEfficely.1)Itsupportsbothlittle-endianandBig-endianBig-endianbyteorders,CompialforOss-System-System-System-compatibility.2)

Go Byte Slice操纵教程:掌握'字节”软件包Go Byte Slice操纵教程:掌握'字节”软件包May 12, 2025 am 12:02 AM

掌握Go语言中的bytes包有助于提高代码的效率和优雅性。1)bytes包对于解析二进制数据、处理网络协议和内存管理至关重要。2)使用bytes.Buffer可以逐步构建字节切片。3)bytes包提供了搜索、替换和分割字节切片的功能。4)bytes.Reader类型适用于从字节切片读取数据,特别是在I/O操作中。5)bytes包与Go的垃圾回收器协同工作,提高了大数据处理的效率。

您如何使用'字符串”软件包在GO中操纵字符串?您如何使用'字符串”软件包在GO中操纵字符串?May 12, 2025 am 12:01 AM

你可以使用Go语言中的"strings"包来操纵字符串。1)使用strings.TrimSpace去除字符串两端的空白字符。2)用strings.Split将字符串按指定分隔符拆分成切片。3)通过strings.Join将字符串切片合并成一个字符串。4)用strings.Contains检查字符串是否包含特定子串。5)利用strings.ReplaceAll进行全局替换。注意使用时要考虑性能和潜在的陷阱。

如何使用'字节”软件包在GO中操纵字节切片(逐步)如何使用'字节”软件包在GO中操纵字节切片(逐步)May 12, 2025 am 12:01 AM

ThebytespackageinGoishighlyeffectiveforbyteslicemanipulation,offeringfunctionsforsearching,splitting,joining,andbuffering.1)Usebytes.Containstosearchforbytesequences.2)bytes.Splithelpsbreakdownbyteslicesusingdelimiters.3)bytes.Joinreconstructsbytesli

Go Bytes软件包:有什么选择?Go Bytes软件包:有什么选择?May 11, 2025 am 12:11 AM

thealternativestogo'sbytespackageincageincludethestringspackage,bufiopackage和customstructs.1)thestringspackagecanbeusedforbytemanipulationforbytemanipulationbybyconvertingbytestostostostostostrings.2))

操纵字节切片在GO:'字节”软件包的功能操纵字节切片在GO:'字节”软件包的功能May 11, 2025 am 12:09 AM

“字节”包装封装forefforeflyManipulatingByteslices,CocialforbinaryData,网络交易和andfilei/o.itoffersfunctionslikeIndexForsearching,BufferForhandLinglaRgedLargedLargedAtaTasets,ReaderForsimulatingStreamReadReadImreAmreadReamReadinging,以及Joineffiter和Joineffiter和Joineffore

Go Strings套餐:弦乐操纵的综合指南Go Strings套餐:弦乐操纵的综合指南May 11, 2025 am 12:08 AM

go'sstringspackageIscialforficientficientsTringManipulation,uperingToolSlikestrings.split(),strings.join(),strings.replaceall(),andStrings.contains.contains.contains.contains.contains.contains.split.split(split()strings.split()dividesStringoSubSubStrings; 2)strings.joins.joins.joinsillise.joinsinelline joinsiline joinsinelline; 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版本,支持代码提示!

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

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

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器