I'm trying to make this loop restart every time a name is already in the list, this code is obviously only going to check this once. Is there any way to make the loop restart from beginning? Thanks!
for _, client := range list.clients {
//for i := 0; i < len(list.clients); i++ {
if(client.name==name){
connection.Write([]byte("Name already exists please try another one:
"))
bytesRead, _ := connection.Read(reply)
name = string(reply[0:bytesRead])
name = strings.TrimSuffix(name, "
")
}
}
Wrap it in another for
:
Loop:
for {
for _, client := range list.clients {
if client.name == name {
connection.Write([]byte("Name already exists please try another one:
"))
bytesRead, _ := connection.Read(reply)
name = string(reply[0:bytesRead])
name = strings.TrimSuffix(name, "
")
continue Loop // Start over
}
}
break // Got through it; we're done
}
You can also just reset your index. range
may be the wrong tool here:
for i := 0; i < len(list.clients); i++ {
client := list.clients[i]
if client.name == name {
connection.Write([]byte("Name already exists please try another one:
"))
bytesRead, _ := connection.Read(reply)
name = string(reply[0:bytesRead])
name = strings.TrimSuffix(name, "
")
i = -1 // Start again
}
}