Modify Slice

...

Go Slice'lar bilan ishlash: Access, Change, Append va Copy

Slice Elementlariga Murojaat Qilish

Slice'ning ma'lum bir elementiga indeks raqami orqali murojaat qilish mumkin.

Go tilida indekslar 0 dan boshlanadi. Ya'ni, [0] birinchi element, [1] ikkinchi element va hokazo.

Misol Bu misolda prices slice'dagi birinchi va uchinchi elementlarga qanday murojaat qilish ko'rsatilgan:

package main
import ("fmt")
 
func main() {
  prices := []int{10,20,30}
 
  fmt.Println(prices[0])
  fmt.Println(prices[2])
}

Natija:

10
30

Slice Elementlarini O'zgartirish

Indeks raqami orqali slice'ning ma'lum bir elementini ham o'zgartirish mumkin.

Misol Bu misol prices slice'dagi uchinchi elementni qanday o'zgartirishni ko'rsatadi:

package main
import ("fmt")
 
func main() {
  prices := []int{10,20,30}
  prices[2] = 50
  fmt.Println(prices[0])
  fmt.Println(prices[2])
}

Natija:

10
50

Slice'ga Element Qo'shish (Append)

append() funktsiyasi yordamida slice oxiriga elementlar qo'shish mumkin.

Sintaksis

slice_nomi = append(slice_nomi, element1, element2, ...)

Misol Bu misolda slice oxiriga qanday element qo'shish ko'rsatiladi:

package main
import ("fmt")
 
func main() {
  myslice1 := []int{1, 2, 3, 4, 5, 6}
  fmt.Printf("myslice1 = %v\n", myslice1)
  fmt.Printf("length = %d\n", len(myslice1))
  fmt.Printf("capacity = %d\n", cap(myslice1))
 
  myslice1 = append(myslice1, 20, 21)
  fmt.Printf("myslice1 = %v\n", myslice1)
  fmt.Printf("length = %d\n", len(myslice1))
  fmt.Printf("capacity = %d\n", cap(myslice1))
}

Natija:

myslice1 = [1 2 3 4 5 6]
length = 6
capacity = 6
myslice1 = [1 2 3 4 5 6 20 21]
length = 8
capacity = 12

Bitta Slice'ni Ikkinchi Slice'ga Qo'shish

Bitta slice'dagi barcha elementlarni ikkinchi slice'ga qo'shish uchun append() funktsiyasidan foydalaning.

Sintaksis

slice3 = append(slice1, slice2...)

Eslatma: slice2 dan keyin ... qo'shilishi kerak.

Misol Bu misol bitta slice'ni ikkinchisiga qanday qo'shishni ko'rsatadi:

package main
import ("fmt")
 
func main() {
  myslice1 := []int{1,2,3}
  myslice2 := []int{4,5,6}
  myslice3 := append(myslice1, myslice2...)
 
  fmt.Printf("myslice3=%v\n", myslice3)
  fmt.Printf("length=%d\n", len(myslice3))
  fmt.Printf("capacity=%d\n", cap(myslice3))
}

Natija:

myslice3=[1 2 3 4 5 6]
length=6
capacity=6

Slice Uzunligini O'zgartirish

Massivlardan farqli o'laroq, slice'ning uzunligini o'zgartirish mumkin.

Misol Bu misol slice uzunligini qanday o'zgartirishni ko'rsatadi:

package main
import ("fmt")
 
func main() {
  arr1 := [6]int{9, 10, 11, 12, 13, 14} // Massiv
  myslice1 := arr1[1:5] // Slice massivdan kesilgan
  fmt.Printf("myslice1 = %v\n", myslice1)
  fmt.Printf("length = %d\n", len(myslice1))
  fmt.Printf("capacity = %d\n", cap(myslice1))
 
  myslice1 = arr1[1:3] // Uzunlikni qayta slice qilish orqali o'zgartirish
  fmt.Printf("myslice1 = %v\n", myslice1)
  fmt.Printf("length = %d\n", len(myslice1))
  fmt.Printf("capacity = %d\n", cap(myslice1))
 
  myslice1 = append(myslice1, 20, 21, 22, 23) // Elementlar qo'shish orqali uzunlikni o'zgartirish
  fmt.Printf("myslice1 = %v\n", myslice1)
  fmt.Printf("length = %d\n", len(myslice1))
  fmt.Printf("capacity = %d\n", cap(myslice1))
}

Natija:

myslice1 = [10 11 12 13]
length = 4
capacity = 5
myslice1 = [10 11]
length = 2
capacity = 5
myslice1 = [10 11 20 21 22 23]
length = 6
capacity = 10

Xotira Tejamkorligi

Slice'lar bilan ishlaganda, Go barcha ichki elementlarni xotiraga yuklaydi. Agar massiv katta bo'lsa va sizga faqat bir nechta element kerak bo'lsa, ularni copy() funktsiyasi yordamida ko'chirish ma'qul.

copy() funktsiyasi slice uchun kerakli elementlar bilan yangi massiv yaratadi, bu dastur ishlatadigan xotirani kamaytiradi.

Sintaksis

copy(dest, src)

copy() funktsiyasi ikkita slice oladi: dest va src, va src dan dest ga ma'lumotlarni ko'chiradi. Ko'chirilgan elementlar sonini qaytaradi.

Misol Bu misol copy() funktsiyasidan qanday foydalanishni ko'rsatadi:

package main
import ("fmt")
 
func main() {
  numbers := []int{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
  // Asl slice
  fmt.Printf("numbers = %v\n", numbers)
  fmt.Printf("length = %d\n", len(numbers))
  fmt.Printf("capacity = %d\n", cap(numbers))
 
  // Faqat kerakli raqamlar bilan nusxa yaratish
  neededNumbers := numbers[:len(numbers)-10]
  numbersCopy := make([]int, len(neededNumbers))
  copy(numbersCopy, neededNumbers)
 
  fmt.Printf("numbersCopy = %v\n", numbersCopy)
  fmt.Printf("length = %d\n", len(numbersCopy))
  fmt.Printf("capacity = %d\n", cap(numbersCopy))
}

Natija:

// Asl slice
numbers = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
length = 15
capacity = 15
// Yangi slice
numbersCopy = [1 2 3 4 5]
length = 5
capacity = 5

Yangi slice sig'imi asl slice'dan kichikroq bo'ldi, chunki yangi massiv kichikroq.

Ushbu sahifada

Xato haqida xabar berish