Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Weird merge slices

  • When we try to remove an element in the middle but assign to a new name.
  • Avoid this mess!
package main

import (
	"fmt"
)

func main() {
	celestialObjects := []string{"Moon", "Gas", "Asteroid", "Dwarf", "Star", "Commet"}
	fmt.Println(celestialObjects)

	fmt.Println(celestialObjects[:3])
	fmt.Println(celestialObjects[4:])
	fmt.Println()

	part := append(celestialObjects[:3], celestialObjects[4:]...)
	fmt.Println(part)
	fmt.Println(celestialObjects)
	fmt.Printf("part - len: %v cap: %v\n", len(part), cap(part))
	fmt.Printf("orig - len: %v cap: %v\n", len(celestialObjects), cap(celestialObjects))
}
[Moon Gas Asteroid Dwarf Star Commet]
[Moon Gas Asteroid]
[Star Commet]

[Moon Gas Asteroid Star Commet]
[Moon Gas Asteroid Star Commet Commet]