如何在字符串数组上循环

How to loop on array of string

我有一个用 golang 实现的命令行工具,它工作正常。 我想执行一些应该提供字符串列表的命令

apps := $(shell fxt run apps)
apps:
    @echo $(apps) is called

在终端中,我在执行 make 时看到以下内容(完全没问题)

[app1 app2] is called

由于命令 fxt run apps return 字符串数组 (var apps []string)

我的问题是如何循环 apps 变量?

通过命令 returned 的数据很好,但现在我需要获取这个列表 (app1...appN) 并循环遍历它,这个问题我不清楚,我如何遍历字符串数组?

特殊情况 如果在循环列表中我得到 app7 如何在代码中进行分叉,例如 if(app7) 打印 mvn clean install

示例。

对于每个应用程序(在应用程序列表中)我需要运行命令

go test ./...

但是app7需要运行

mvn clean install

对于 app10

yarn

您想 运行 您的循环在 make 本身或实际上正在执行 shell 的配方中?两者都有!

备注:我替换执行的命令自己测试。这里我使用 ls 来填充我的数组。

apps := $(shell ls)

#looping in make itself
$(foreach var,$(apps),$(info In the loop running with make: $(var)))

#loop in shell inside recipe 
go:
    @for v in $(apps) ; do \
        echo inside recipe loop with sh command: $$v ; \
    done

输出:

In the loop running with make: a
In the loop running with make: b
In the loop running with make: c
In the loop running with make: Makefile
inside recipe loop with sh command: a
inside recipe loop with sh command: b
inside recipe loop with sh command: c
inside recipe loop with sh command: Makefile