Skip to content

Iterate over list of string using split or strings.Split #1535

Answered by hairyhenderson
air3ijai asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @air3ijai, thanks for taking the time to post this.

[...] And it looks like strings.Split works only with a pipe-line, [...]

No, it works both ways. The pipeline syntax is just syntactic sugar for providing the left-hand side of the | as the right-most argument.

I see in your example you've flipped the order of the two arguments, however:

# strings.Split
gomplate -i '{{ strings.Split .Env.REGIONS " " }}'
[ ]

What you actually want is {{ strings.Split " " .Env.REGIONS }}.

As for the difference between strings.Split and split, this is unfortunately by design. If you look at the documentation for each, you'll see split is not an alias for strings.Split, and split does redirect you to u…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@hairyhenderson
Comment options

Answer selected by air3ijai
Comment options

You must be logged in to vote
1 reply
@hairyhenderson
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants