Insertar y Concatenar Elementos en un Slice en GO (Golang)


<p>package main</p>
<p>import (<br>     "fmt"<br>
)</p>
<p>func main() {<br>     arr1 := []int{32, 57, 35, 22}<br>     arr2 := []int{32, 57, 35, 22}</p>
<p>    arr1 = append(arr1, 0)   // Making space for the new element<br>     copy(arr1[3:], arr1[2:]) // Shifting elements<br>     arr1[2] = 99             // Copying/inserting the value</p>
<p>    fmt.Println(arr1)        // Printing Result<br>     <br>     arr2 = append(arr2, 0)   // Making space for the new element<br>     insert(arr2, 99, 2)     // Another way to do it<br>     <br>     fmt.Println(arr2)        // Same result<br>
}</p>
<p>func insert(array []int, element int, i int) []int {<br>     return append(array[:i], append([]int{element}, array[i:]...)...)<br>
}</p>

<p>
append(s S, x ...T) S  // T is the element type of S</p><p>s0 := []int{0, 0}<br>s1 := append(s0, 2)        // append a single element     s1 == []int{0, 0, 2}<br>s2 := append(s1, 3, 5, 7)  // append multiple elements    s2 == []int{0, 0, 2, 3, 5, 7}<br>s3 := append(s2, s0...)    // append a slice              s3 == []int{0, 0, 2, 3, 5, 7, 0, 0}</p>

About AVB

Check Also

Cómo usar mapas de folium en PyQt5 – Python

Folium tiene la capacidad de guardar los mapas resultados en un archivo html para ser …

Leave a Reply

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