What happens if a slice exceeds its capacity while appending elements in Go?

  • A new underlying array is allocated, and elements are copied over.
  • An error is thrown.
  • Elements are automatically truncated to fit the capacity.
  • It leads to a runtime panic.
In Go, if a slice exceeds its capacity while appending elements, a new underlying array with a larger capacity is allocated, and the existing elements are copied over. This ensures that the slice can accommodate the new elements without causing a runtime error.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *