diff --git a/SliceTricks.md b/SliceTricks.md index 20cc0331..78dea5fc 100644 --- a/SliceTricks.md +++ b/SliceTricks.md @@ -234,15 +234,15 @@ result := in[:j+1] fmt.Println(result) // [1 2 3 4] ``` -### Move to front, or append if not present, in place +### Move to front, or prepend if not present, in place if possible. ```go // moveToFront moves needle to the front of haystack, in place if possible. func moveToFront(needle string, haystack []string) []string { - if len(haystack) == 0 || haystack[0] == needle { + if len(haystack) != 0 && haystack[0] == needle { return haystack } - var prev string + prev := needle for i, elem := range haystack { switch { case i == 0: