الإلحاق بشريحة
الإلحاق بشريحة
من الشائع إلحاق عناصر جديدة بشريحة، ولذلك توفّر Go دالة مدمجة
هي append. ويصف توثيق
الحزمة المدمجة الدالة append.
func append(s []T, vs ...T) []T
المعامل الأول s للدالة append هو شريحة من النوع T، والبقية هي
قيم من النوع T تُلحَق بالشريحة.
القيمة الناتجة من append هي شريحة تحتوي على جميع عناصر
الشريحة الأصلية، بالإضافة إلى القيم المقدَّمة.
إذا كانت المصفوفة التي تستند إليها s أصغر من أن تستوعب جميع القيم المعطاة، فستُخصَّص ذاكرة لمصفوفة أكبر.
وستشير الشريحة المُعادة إلى المصفوفة التي خُصّصت لها الذاكرة
حديثًا.
(لمعرفة المزيد عن الشرائح، اقرأ مقالة الشرائح: الاستخدام وآلية العمل الداخلية.)
النص الأصلي بالإنجليزية
Appending to a slice
It is common to append new elements to a slice, and so Go provides a built-in
append function. The documentation
of the built-in package describes append.
func append(s []T, vs ...T) []T
The first parameter s of append is a slice of type T, and the rest are
T values to append to the slice.
The resulting value of append is a slice containing all the elements of the
original slice plus the provided values.
If the backing array of s is too small to fit all the given values a bigger
array will be allocated. The returned slice will point to the newly allocated
array.
(To learn more about slices, read the Slices: usage and internals article.)
هذا الدرس للقراءة فقط؛ جرّب المثال في الدرس التالي.