Skip to content

Conversation

@javorszky
Copy link
Contributor

Closes #8095

Per testing and documentation the if guard to check if a slice is not empty or not nil is not necessary.

Documentation:

{{range pipeline}} T1 {{end}}
The value of the pipeline must be an array, slice, map, iter.Seq,
iter.Seq2, integer or channel.
If the value of the pipeline has length zero, nothing is output;
otherwise, dot is set to the successive elements of the array,
slice, or map and T1 is executed. If the value is a map and the
keys are of basic type with a defined order, the elements will be
visited in sorted key order.

https://pkg.go.dev/text/template#hdr-Actions

Code testing:

<p>begin</p> {{ range $value := .Elements }} <p>{{ $value }}</p> {{ end }} <p>end</p> 

struct that gets populated with data:

type Example struct { Elements []string } 

executed with the following data:

// slice is not empty e := Example{ Elements: []string{"one", "two", "three"} } // slice is empty, but initialised eEmpty := Example { Elements: []string{} } // slice is nil eNil := Example { Elements: nil } 

The output for these three in order:

<p>begin</p> <p>Element 1</p> <p>Element 2</p> <p>Element 3</p> <p>end</p> <p>begin</p> <p>end</p> <p>begin</p> <p>end</p> 

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked that all unit tests pass after adding my changes
  • I have updated necessary documentation
  • I have rebased my branch onto main
  • I will ensure my PR is targeting the main branch and pulling from my branch from my own fork
Closes #8095 Per testing and documentation the if guard to check if a slice is not empty or not nil is not necessary. Documentation: > {{range pipeline}} T1 {{end}} >	The value of the pipeline must be an array, slice, map, iter.Seq, >	iter.Seq2, integer or channel. >	If the value of the pipeline has length zero, nothing is output; >	otherwise, dot is set to the successive elements of the array, >	slice, or map and T1 is executed. If the value is a map and the >	keys are of basic type with a defined order, the elements will be >	visited in sorted key order. https://pkg.go.dev/text/template#hdr-Actions Code testing: ``` <p>begin</p> {{ range $value := .Elements }} <p>{{ $value }}</p> {{ end }} <p>end</p> ``` struct that gets populated with data: ``` type Example struct { Elements []string } ``` executed with the following data: ``` // slice is not empty e := Example{ Elements: []string{"one", "two", "three"} } // slice is empty, but initialised eEmpty := Example { Elements: []string{} } // slice is nil eNil := Example { Elements: nil } ``` The output for these three in order: ``` <p>begin</p> <p>Element 1</p> <p>Element 2</p> <p>Element 3</p> <p>end</p> <p>begin</p> <p>end</p> <p>begin</p> <p>end</p> ```
@javorszky javorszky requested a review from a team as a code owner August 21, 2025 16:39
@github-actions github-actions bot added the chore Pull requests for routine tasks label Aug 22, 2025
@javorszky javorszky merged commit 2dd6ed3 into main Aug 22, 2025
88 of 94 checks passed
@javorszky javorszky deleted the chore/8095-template-range-if-guard branch August 22, 2025 13:19
AlexFenlon pushed a commit that referenced this pull request Sep 9, 2025
Closes #8095 Per testing and documentation the if guard to check if a slice is not empty or not nil is not necessary. Documentation: > {{range pipeline}} T1 {{end}} >	The value of the pipeline must be an array, slice, map, iter.Seq, >	iter.Seq2, integer or channel. >	If the value of the pipeline has length zero, nothing is output; >	otherwise, dot is set to the successive elements of the array, >	slice, or map and T1 is executed. If the value is a map and the >	keys are of basic type with a defined order, the elements will be >	visited in sorted key order. https://pkg.go.dev/text/template#hdr-Actions Code testing: ``` <p>begin</p> {{ range $value := .Elements }} <p>{{ $value }}</p> {{ end }} <p>end</p> ``` struct that gets populated with data: ``` type Example struct { Elements []string } ``` executed with the following data: ``` // slice is not empty e := Example{ Elements: []string{"one", "two", "three"} } // slice is empty, but initialised eEmpty := Example { Elements: []string{} } // slice is nil eNil := Example { Elements: nil } ``` The output for these three in order: ``` <p>begin</p> <p>Element 1</p> <p>Element 2</p> <p>Element 3</p> <p>end</p> <p>begin</p> <p>end</p> <p>begin</p> <p>end</p> ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Pull requests for routine tasks

4 participants