Skip to content

Commit 028a955

Browse files
authored
refactor: use range-based loops (#514)
Replace explicit index iterations with range-style for-loops to simplify and idiomatize loop constructs. These edits are aimed at clearer, more idiomatic Go iteration. Signed-off-by: Ryan Johnson <ryan@tenthirtyam.org>
1 parent 29ab4ac commit 028a955

5 files changed

Lines changed: 11 additions & 11 deletions

File tree

builder/vmware/common/cdrom_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func FindNextAvailableCDROMSlot(vmxData map[string]string, adapterType string) (
5151
}
5252

5353
// Find the next available slot.
54-
for busNum := 0; busNum < 4; busNum++ { // VMware supports up to 4 buses for most adapter types
54+
for busNum := range 4 { // VMware supports up to 4 buses for most adapter types
5555
busStr := strconv.Itoa(busNum)
5656
busSlots := usedSlots[busStr]
5757

builder/vmware/common/driver_parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1994,7 +1994,7 @@ func flattenNetworkingConfig(in chan networkingCommandEntry) NetworkingConfig {
19941994
vmnet = e.removeNatPrefix.vnet
19951995
prefixes, exists := result.natPrefix[vmnet]
19961996
if exists {
1997-
for index := 0; index < len(prefixes); index++ {
1997+
for index := range prefixes {
19981998
if prefixes[index] == e.removeNatPrefix.prefix {
19991999
result.natPrefix[vmnet] = append(prefixes[:index], prefixes[index+1:]...)
20002000
break

builder/vmware/common/driver_parser_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ func TestParserTokenizeNetworkingConfig(t *testing.T) {
10791079
{"newline-less"},
10801080
}
10811081

1082-
for testnum := 0; testnum < len(tests); testnum++ {
1082+
for testnum := range tests {
10831083
inCh := consumeString(tests[testnum])
10841084
outCh := tokenizeNetworkingConfig(inCh)
10851085
result := collectIntoStringList(outCh)
@@ -1091,7 +1091,7 @@ func TestParserTokenizeNetworkingConfig(t *testing.T) {
10911091
}
10921092

10931093
ok := true
1094-
for index := 0; index < len(expected); index++ {
1094+
for index := range expected {
10951095
if result[index] != expected[index] {
10961096
ok = false
10971097
}
@@ -1116,7 +1116,7 @@ func TestParserSplitNetworkingConfig(t *testing.T) {
11161116
{"and", "begin", "with", "an", "empty", "string"},
11171117
}
11181118

1119-
for testnum := 0; testnum < len(tests); testnum++ {
1119+
for testnum := range tests {
11201120
inCh := consumeString(tests[testnum])
11211121
stringCh := tokenizeNetworkingConfig(inCh)
11221122
outCh := splitNetworkingConfig(stringCh)
@@ -1133,7 +1133,7 @@ func TestParserSplitNetworkingConfig(t *testing.T) {
11331133
}
11341134

11351135
ok := true
1136-
for index := 0; index < len(expected); index++ {
1136+
for index := range expected {
11371137
if result[index] != expected[index] {
11381138
ok = false
11391139
}
@@ -1152,14 +1152,14 @@ func TestParserParseNetworkingConfigVersion(t *testing.T) {
11521152
"VERSION=a,b",
11531153
}
11541154

1155-
for testnum := 0; testnum < len(successTests); testnum++ {
1155+
for testnum := range successTests {
11561156
test := []string{successTests[testnum]}
11571157
if _, err := networkingReadVersion(test); err != nil {
11581158
t.Errorf("success-test %d parsing failed: %v", 1+testnum, err)
11591159
}
11601160
}
11611161

1162-
for testnum := 0; testnum < len(successTests); testnum++ {
1162+
for testnum := range successTests {
11631163
test := []string{failureTests[testnum]}
11641164
if _, err := networkingReadVersion(test); err == nil {
11651165
t.Errorf("failure-test %d should have failed", 1+testnum)
@@ -1181,7 +1181,7 @@ func TestParserParseNetworkingConfigEntries(t *testing.T) {
11811181
"remove_nat_prefix 57005 /31",
11821182
}
11831183

1184-
for testnum := 0; testnum < len(tests); testnum++ {
1184+
for testnum := range tests {
11851185
test := strings.Split(tests[testnum], " ")
11861186
parser := NetworkingParserByCommand(test[0])
11871187
if parser == nil {

builder/vmware/common/step_configure_vnc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func VNCPassword(skipPassword bool) string {
6868

6969
password := make([]byte, length)
7070

71-
for i := 0; i < length; i++ {
71+
for i := range length {
7272
password[i] = charSet[rand.Intn(charSetLength)]
7373
}
7474

builder/vmware/common/step_output_dir.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (s *StepOutputDir) Cleanup(state multistep.StateBag) {
9797
exists, _ := dir.DirExists()
9898
if exists {
9999
ui.Say("Deleting output directory...")
100-
for i := 0; i < 5; i++ {
100+
for range 5 {
101101
err := dir.RemoveAll()
102102
if err == nil {
103103
break

0 commit comments

Comments
 (0)