El segmento de lenguaje de Go es más poderoso, flexible y conveniente que una array y es una estructura de datos liviana. Slice es una secuencia de longitud variable que almacena elementos de un tipo similar, no está permitido almacenar diferentes tipos de elementos en el mismo segmento. Es como una array que tiene un valor de índice y una longitud, pero el tamaño del segmento cambia de tamaño, no tienen un tamaño fijo como una array. Internamente, un segmento y un arreglo están conectados entre sí, un segmento es una referencia a un arreglo subyacente. Se permite almacenar elementos duplicados en el segmento. La primera posición del índice en un segmento siempre es 0 y la última será (longitud del segmento – 1) .
Declaración de Rebanada
Un segmento se declara como una array, pero no contiene el tamaño del segmento. Por lo que puede crecer o encoger según el requisito.
Go
// Golang program to illustrate // the working of the slice components package main import "fmt" func main() { // Creating an array arr := [7]string{"This", "is", "the", "tutorial", "of", "Go", "language"} // Display array fmt.Println("Array:", arr) // Creating a slice myslice := arr[1:6] // Display slice fmt.Println("Slice:", myslice) // Display length of the slice fmt.Printf("Length of the slice: %d", len(myslice)) // Display the capacity of the slice fmt.Printf("\nCapacity of the slice: %d", cap(myslice)) }
Go
// Golang program to illustrate how // to create a slice using a slice // literal package main import "fmt" func main() { // Creating a slice // using the var keyword var my_slice_1 = []string{"Geeks", "for", "Geeks"} fmt.Println("My Slice 1:", my_slice_1) // Creating a slice //using shorthand declaration my_slice_2 := []int{12, 45, 67, 56, 43, 34, 45} fmt.Println("My Slice 2:", my_slice_2) }
Go
// Golang program to illustrate how to // create slices from the array package main import "fmt" func main() { // Creating an array arr := [4]string{"Geeks", "for", "Geeks", "GFG"} // Creating slices from the given array var my_slice_1 = arr[1:2] my_slice_2 := arr[0:] my_slice_3 := arr[:2] my_slice_4 := arr[:] // Display the result fmt.Println("My Array: ", arr) fmt.Println("My Slice 1: ", my_slice_1) fmt.Println("My Slice 2: ", my_slice_2) fmt.Println("My Slice 3: ", my_slice_3) fmt.Println("My Slice 4: ", my_slice_4) }
Go
// Golang program to illustrate how to // create slices from the slice package main import "fmt" func main() { // Creating s slice oRignAl_slice := []int{90, 60, 40, 50, 34, 49, 30} // Creating slices from the given slice var my_slice_1 = oRignAl_slice[1:5] my_slice_2 := oRignAl_slice[0:] my_slice_3 := oRignAl_slice[:6] my_slice_4 := oRignAl_slice[:] my_slice_5 := my_slice_3[2:4] // Display the result fmt.Println("Original Slice:", oRignAl_slice) fmt.Println("New Slice 1:", my_slice_1) fmt.Println("New Slice 2:", my_slice_2) fmt.Println("New Slice 3:", my_slice_3) fmt.Println("New Slice 4:", my_slice_4) fmt.Println("New Slice 5:", my_slice_5) }
Go
// Go program to illustrate how to create slices // Using make function package main import "fmt" func main() { // Creating an array of size 7 // and slice this array till 4 // and return the reference of the slice // Using make function var my_slice_1 = make([]int, 4, 7) fmt.Printf("Slice 1 = %v, \nlength = %d, \ncapacity = %d\n", my_slice_1, len(my_slice_1), cap(my_slice_1)) // Creating another array of size 7 // and return the reference of the slice // Using make function var my_slice_2 = make([]int, 7) fmt.Printf("Slice 2 = %v, \nlength = %d, \ncapacity = %d\n", my_slice_2, len(my_slice_2), cap(my_slice_2)) }
Go
// Golang program to illustrate the // iterating over a slice using // for loop package main import "fmt" func main() { // Creating a slice myslice := []string{"This", "is", "the", "tutorial", "of", "Go", "language"} // Iterate using for loop for e := 0; e < len(myslice); e++ { fmt.Println(myslice[e]) } }
Go
// Golang program to illustrate the iterating // over a slice using range in for loop package main import "fmt" func main() { // Creating a slice myslice := []string{"This", "is", "the", "tutorial", "of", "Go", "language"} // Iterate slice // using range in for loop for index, ele := range myslice { fmt.Printf("Index = %d and element = %s\n", index+3, ele) } }
Go
// Golang program to illustrate the iterating over // a slice using range in for loop without an index package main import "fmt" func main() { // Creating a slice myslice := []string{"This", "is", "the", "tutorial", "of", "Go", "language"} // Iterate slice // using range in for loop // without index for _, ele := range myslice { fmt.Printf("Element = %s\n", ele) } }
Go
// Go program to illustrate a zero value slice package main import "fmt" func main() { // Creating a zero value slice var myslice []string fmt.Printf("Length = %d\n", len(myslice)) fmt.Printf("Capacity = %d ", cap(myslice)) }
Go
// Golang program to illustrate // how to modify the slice package main import "fmt" func main() { // Creating a zero value slice arr := [6]int{55, 66, 77, 88, 99, 22} slc := arr[0:4] // Before modifying fmt.Println("Original_Array: ", arr) fmt.Println("Original_Slice: ", slc) // After modification slc[0] = 100 slc[1] = 1000 slc[2] = 1000 fmt.Println("\nNew_Array: ", arr) fmt.Println("New_Slice: ", slc) }
Go
// Golang program to check if // the slice is nil or not package main import "fmt" func main() { // creating slices s1 := []int{12, 34, 56} var s2 []int // If you try to run this commented // code compiler will give an error /*s3:= []int{23, 45, 66} fmt.Println(s1==s3) */ // Checking if the given slice is nil or not fmt.Println(s1 == nil) fmt.Println(s2 == nil) }
Go
// Go program to illustrate the multi-dimensional slice package main import "fmt" func main() { // Creating multi-dimensional slice s1 := [][]int{{12, 34}, {56, 47}, {29, 40}, {46, 78}, } // Accessing multi-dimensional slice fmt.Println("Slice 1 : ", s1) // Creating multi-dimensional slice s2 := [][]string{ []string{"Geeks", "for"}, []string{"Geeks", "GFG"}, []string{"gfg", "geek"}, } // Accessing multi-dimensional slice fmt.Println("Slice 2 : ", s2) }
Go
// Go program to illustrate how to sort // the elements present in the slice package main import ( "fmt" "sort" ) func main() { // Creating Slice slc1 := []string{"Python", "Java", "C#", "Go", "Ruby"} slc2 := []int{45, 67, 23, 90, 33, 21, 56, 78, 89} fmt.Println("Before sorting:") fmt.Println("Slice 1: ", slc1) fmt.Println("Slice 2: ", slc2) // Performing sort operation on the // slice using sort function sort.Strings(slc1) sort.Ints(slc2) fmt.Println("\nAfter sorting:") fmt.Println("Slice 1: ", slc1) fmt.Println("Slice 2: ", slc2) }
Publicación traducida automáticamente
Artículo escrito por ankita_saini y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA