Junior — Middle
Kas saaksite selgitada andmestruktuuri, näiteks nimekirja või massiivi, lõppu elementide lisamise mehhanismi?
sobes.tech AI
Vastus AI-lt
In Go, to add an element to the end of a slice, the built-in append function is used. Slices are dynamic structures that can change size.
Example:
package main
import "fmt"
func main() {
nums := []int{1, 2, 3}
nums = append(nums, 4) // adds 4 to the end
fmt.Println(nums) // [1 2 3 4]
}
If the slice does not have enough capacity, append will create a new array with increased size, copy the old elements, and add the new one. Arrays in Go have a fixed size, so slices are used for dynamic addition.