Go provides several built-in functions to work with slices, making it easier to manipulate and manage slices dynamically. Some common slice operations include append
, copy
, len
, cap
, and others. These operations enable flexible and efficient handling of slices.
1️⃣ Append Operation
The append()
function is used to add elements to a slice. It appends one or more elements to the end of a slice, automatically increasing the slice's size if necessary.
🔹 Example:
package main
import "fmt"
func main() {
slice := []int{1, 2, 3}
// Append elements to the slice
slice = append(slice, 4, 5)
fmt.Println(slice) // Output: [1 2 3 4 5]
}
🔹 Output:
[1 2 3 4 5]
👉 In this example, the append
function adds 4
and 5
to the slice, dynamically increasing the slice's size.
2️⃣ Copy Operation
The copy()
function copies elements from one slice into another. It copies the elements from the source slice to the destination slice and returns the number of elements copied.
🔹 Example:
package main
import "fmt"
func main() {
slice1 := []int{1, 2, 3}
slice2 := make([]int, len(slice1))
// Copy elements from slice1 to slice2
copy(slice2, slice1)
fmt.Println(slice2) // Output: [1 2 3]
}
🔹 Output:
[1 2 3]
👉 Here, the copy
function copies the contents of slice1
into slice2
. Both slices now contain the same elements.
3️⃣ Length and Capacity
len()
function: Returns the number of elements in the slice.cap()
function: Returns the total number of elements the slice can hold before it needs to resize (its capacity).🔹 Example:
package main
import "fmt"
func main() {
slice := make([]int, 3, 5) // length 3, capacity 5
fmt.Println("Length:", len(slice)) // Output: Length: 3
fmt.Println("Capacity:", cap(slice)) // Output: Capacity: 5
}
🔹 Output:
Length: 3
Capacity: 5
👉 Here, the slice has a length of 3 and a capacity of 5. The capacity allows it to hold 5 elements before resizing.
4️⃣ Slicing a Slice
You can create sub-slices (or slices from existing slices) using slice notation. This allows you to extract portions of a slice.
🔹 Example:
package main
import "fmt"
func main() {
slice := []int{10, 20, 30, 40, 50}
// Create a sub-slice from index 1 to 3
subSlice := slice[1:4]
fmt.Println(subSlice) // Output: [20 30 40]
}
🔹 Output:
[20 30 40]
👉 The sub-slice subSlice
is created from index 1
to 3
of the original slice
.
5️⃣ Zeroing a Slice
You can "zero" a slice, meaning setting all its elements to the zero value for its type (e.g., 0
for int
, false
for bool
, ""
for string
, etc.).
🔹 Example:
package main
import "fmt"
func main() {
slice := []int{1, 2, 3}
// Zero the slice elements
for i := range slice {
slice[i] = 0
}
fmt.Println(slice) // Output: [0 0 0]
}
🔹 Output:
[0 0 0]
👉 In this example, we iterate over the slice and set each element to 0
.
6️⃣ Deleting Elements from a Slice
Go does not have a built-in delete()
function for slices like it does for maps. However, you can delete elements from a slice by creating a new slice that excludes the element(s) you want to remove.
🔹 Example (Delete at Index):
package main
import "fmt"
func main() {
slice := []int{10, 20, 30, 40, 50}
// Delete element at index 2 (30)
slice = append(slice[:2], slice[3:]...)
fmt.Println(slice) // Output: [10 20 40 50]
}
🔹 Output:
[10 20 40 50]
👉 Here, we delete the element at index 2
(which is 30
) by using slicing and append
to combine the elements before and after index 2
.
7️⃣ Checking if a Slice is Empty
You can check if a slice is empty by comparing its length to zero using len()
.
🔹 Example:
package main
import "fmt"
func main() {
slice := []int{}
if len(slice) == 0 {
fmt.Println("Slice is empty")
} else {
fmt.Println("Slice is not empty")
}
}
🔹 Output:
Slice is empty
👉 In this example, we check if the slice is empty by comparing its length to 0
.
append()
: Adds elements to a slice and dynamically resizes it if necessary.copy()
: Copies elements from one slice to another.len()
and cap()
: Get the length and capacity of a slice.delete()
function for slices, you can remove elements by slicing and appending.len()
to check if a slice is empty.গো-তে স্লাইসের সাথে কাজ করার জন্য অনেকগুলো বিল্ট-ইন ফাংশন রয়েছে, যা স্লাইসের আকার পরিবর্তন এবং উপাদান সংযোজনসহ বিভিন্ন অপারেশন সম্পাদন করতে সাহায্য করে। কিছু সাধারণ স্লাইস অপারেশন হলো append
, copy
, len
, cap
, ইত্যাদি। এই অপারেশনগুলো স্লাইসগুলোর সাথে নমনীয়ভাবে এবং দক্ষভাবে কাজ করতে সহায়ক।
1️⃣ Append অপারেশন
append()
ফাংশন ব্যবহার করে স্লাইসে নতুন উপাদান যোগ করা হয়। এটি এক বা একাধিক উপাদান স্লাইসে যোগ করে এবং স্লাইসের আকার স্বয়ংক্রিয়ভাবে বৃদ্ধি পায়।
🔹 উদাহরণ:
package main
import "fmt"
func main() {
slice := []int{1, 2, 3}
// স্লাইসে উপাদান অ্যাপেন্ড করা
slice = append(slice, 4, 5)
fmt.Println(slice) // আউটপুট: [1 2 3 4 5]
}
🔹 আউটপুট:
[1 2 3 4 5]
👉 এখানে, append
ফাংশন 4
এবং 5
স্লাইসে অ্যাপেন্ড করেছে, এবং স্লাইসটির আকার স্বয়ংক্রিয়ভাবে বৃদ্ধি পেয়েছে।
2️⃣ Copy অপারেশন
copy()
ফাংশন একটি স্লাইস থেকে অন্য স্লাইসে উপাদান কপি করে। এটি সোর্স স্লাইস থেকে ডেস্টিনেশন স্লাইসে উপাদান কপি করে এবং কপি হওয়া উপাদানগুলোর সংখ্যা রিটার্ন করে।
🔹 উদাহরণ:
package main
import "fmt"
func main() {
slice1 := []int{1, 2, 3}
slice2 := make([]int, len(slice1))
// slice1 থেকে slice2-তে উপাদান কপি করা
copy(slice2, slice1)
fmt.Println(slice2) // আউটপুট: [1 2 3]
}
🔹 আউটপুট:
[1 2 3]
👉 এখানে, copy
ফাংশন slice1
থেকে উপাদান কপি করে slice2
তে, এবং দুটি স্লাইস এখন একে অপরের সমান উপাদান ধারণ করছে।
3️⃣ Length এবং Capacity
len()
ফাংশন: স্লাইসে মোট উপাদানের সংখ্যা রিটার্ন করে।cap()
ফাংশন: স্লাইসটি কতটা জায়গা ধারণ করতে পারে (যার ক্ষমতা) তা রিটার্ন করে।🔹 উদাহরণ:
package main
import "fmt"
func main() {
slice := make([]int, 3, 5) // দৈর্ঘ্য 3, ক্ষমতা 5
fmt.Println("Length:", len(slice)) // আউটপুট: Length: 3
fmt.Println("Capacity:", cap(slice)) // আউটপুট: Capacity: 5
}
🔹 আউটপুট:
Length: 3
Capacity: 5
👉 এখানে, স্লাইসটির দৈর্ঘ্য ৩ এবং ক্ষমতা ৫। এর মানে, এটি ৫টি উপাদান ধারণ করতে পারে পুনরায় রিসাইজিংয়ের প্রয়োজন না হলে।
4️⃣ স্লাইস থেকে স্লাইস তৈরি করা (Slicing)
আপনি স্লাইস থেকে একটি সাব-স্লাইস তৈরি করতে পারেন স্লাইস নোটেশনের মাধ্যমে। এটি আপনাকে একটি স্লাইসের অংশ বের করার সুবিধা দেয়।
🔹 উদাহরণ:
package main
import "fmt"
func main() {
slice := []int{10, 20, 30, 40, 50}
// ইনডেক্স ১ থেকে ৩ পর্যন্ত সাব-স্লাইস তৈরি করা
subSlice := slice[1:4]
fmt.Println(subSlice) // আউটপুট: [20 30 40]
}
🔹 আউটপুট:
[20 30 40]
👉 এখানে, subSlice
স্লাইসটি মূল slice
থেকে ইনডেক্স ১ থেকে ৩ পর্যন্ত তৈরি করা হয়েছে।
5️⃣ Slice Zeroing
আপনি একটি স্লাইস "জিরো" (zero) করতে পারেন, এর মানগুলোকে প্রাথমিক শূন্য মানে সেট করে (যেমন, int
এর জন্য 0
, bool
এর জন্য false
, string
এর জন্য ""
, ইত্যাদি)।
🔹 উদাহরণ:
package main
import "fmt"
func main() {
slice := []int{1, 2, 3}
// স্লাইসের সকল উপাদান শূন্য করা
for i := range slice {
slice[i] = 0
}
fmt.Println(slice) // আউটপুট: [0 0 0]
}
🔹 আউটপুট:
[0 0 0]
👉 এখানে, আমরা স্লাইসের প্রতিটি উপাদানকে 0
দিয়ে সেট করেছি।
6️⃣ স্লাইস থেকে উপাদান মুছে ফেলা
গো-তে স্লাইসের জন্য কোনো বিল্ট-ইন delete()
ফাংশন নেই, তবে আপনি স্লাইসের উপাদান মুছে ফেলতে পারেন নতুন স্লাইস তৈরি করে, যাতে আপনি যে উপাদানগুলো মুছতে চান সেগুলো বাদ দিয়ে বাকি অংশ একত্রিত করেন।
🔹 উদাহরণ (ইনডেক্স থেকে উপাদান মুছে ফেলা):
package main
import "fmt"
func main() {
slice := []int{10, 20, 30, 40, 50}
// ইনডেক্স ২ (৩০) থেকে উপাদান মুছে ফেলা
slice = append(slice[:2], slice[3:]...)
fmt.Println(slice) // আউটপুট: [10 20 40 50]
}
🔹 আউটপুট:
[10 20 40 50]
👉 এখানে, আমরা slice[2]
(যেটি 30
ছিল) উপাদানটি মুছে ফেলেছি, এবং নতুন স্লাইস তৈরি করেছি যা পুরানো স্লাইসের প্রথম দুইটি এবং পরবর্তী দুটি উপাদান ধারণ করছে।
7️⃣ স্লাইস খালি কিনা তা চেক করা
আপনি len()
ব্যবহার করে স্লাইস খালি কিনা তা চেক করতে পারেন।
🔹 উদাহরণ:
package main
import "fmt"
func main() {
slice := []int{}
if len(slice) == 0 {
fmt.Println("Slice is empty")
} else {
fmt.Println("Slice is not empty")
}
}
🔹 আউটপুট:
Slice is empty
👉 এখানে, আমরা len()
ব্যবহার করে চেক করেছি যে স্লাইসটি খালি কি না।
append()
: স্লাইসে নতুন উপাদান অ্যাপেন্ড করে এবং আকার স্বয়ংক্রিয়ভাবে বৃদ্ধি পায়।copy()
: একটি স্লাইস থেকে অন্য স্লাইসে উপাদান কপি করে।len()
এবং cap()
: স্লাইসের দৈর্ঘ্য এবং ক্ষমতা নির্ণয় করে।len()
ব্যবহার করে স্লাইসটি খালি কিনা চেক করা হয়।