For the following code, It seems going into dead loop.
After s, err := minusOne(s), s is shorten according the log info.
But the before minus log shows that it never changed.
func minusOne(s string) (string, error) {
if len(s) >= 0 {
return s[1:], nil
}
return "", nil
}
func TestStr(t *testing.T) {
s := "hello world"
for {
log.Println("before minus", s)
s, err := minusOne(s)
log.Println("after minus", s)
if err == nil && len(s) == 0 {
break
}
}
}
If I change it slightly like, it works like expected.
s1, err := minusOne(s)
s = s1
Or if I remove the err returning in the minusOne function, and it also works.
s = minusOne(s)
I really can't understand it. Anyone can help with it?