Code with Go
Ask or search…
K
Links

Slice

This Go program is a tutorial that demonstrates various concepts related to slices, a dynamic array-like data structure in Go. The code covers topics such as slice creation, modification, appending, copying, and working with two-dimensional slices.
Below is a detailed explanation of the code:
package main
import (
"fmt"
"slices"
)
func main() {
// Declare an uninitialized slice of strings
var s []string
// Print the uninitialized slice, its nil status, and its length
fmt.Println("uninit:", s, s == nil, len(s) == 0) // Output: uninit: [] true true
// Create an empty slice of strings with a length of 3 and a capacity of 3
s = make([]string, 3)
fmt.Println("emp:", s, "len:", len(s), "cap:", cap(s)) // Output: emp: [ ] len: 3 cap: 3
// Set values for the first three elements of the slice
s[0] = "a"
s[1] = "b"
s[2] = "c"
fmt.Println("set:", s) // Output: set: [a b c]
// Access the third element of the slice
fmt.Println("get:", s[2]) // Output: get: c
// Print the length of the slice
fmt.Println("len:", len(s)) // Output: len: 3
// Append new elements to the slice
s = append(s, "d")
s = append(s, "e", "f")
fmt.Println("apd:", s) // Output: apd: [a b c d e f]
// Create a new slice `c` with the same length as `s` and copy elements from `s` to `c`
c := make([]string, len(s))
copy(c, s)
fmt.Println("cpy:", c) // Output: cpy: [a b c d e f]
// Slice a portion of `s` from index 2 (inclusive) to index 5 (exclusive)
l := s[2:5]
fmt.Println("sl1:", l) // Output: sl1: [c d e]
// Slice `s` from the beginning (inclusive) to index 5 (exclusive)
l = s[:5]
fmt.Println("sl2:", l) // Output: sl2: [a b c d e]
// Slice `s` from index 2 (inclusive) to the end
l = s[2:]
fmt.Println("sl3:", l) // Output: sl3: [c d e f]
// Declare and initialize a slice `t` with values in a single line
t := []string{"g", "h", "i"}
fmt.Println("dcl:", t) // Output: dcl: [g h i]
// Declare another slice `t2` with the same values as `t`
t2 := []string{"g", "h", "i"}
// Use the `slices.Equal` function to check if `t` and `t2` are equal
if slices.Equal(t, t2) {
fmt.Println("t == t2") // Output: t == t2
}
// Create a 2-dimensional slice of integers
twoD := make([][]int, 3)
for i := 0; i < 3; i++ {
innerLen := i + 1
twoD[i] = make([]int, innerLen)
for j := 0; j < innerLen; j++ {
twoD[i][j] = i + j
}
}
fmt.Println("2d: ", twoD) // Output: 2d: [[0] [1 2] [2 3 4]]
}

Output

uninit: [] true true
emp: [ ] len: 3 cap: 3
set: [a b c]
get: c
len: 3
apd: [a b c d e f]
cpy: [a b c d e f]
sl1: [c d e]
sl2: [a b c d e]
sl3: [c d e f]
dcl: [g h i]
t == t2
2d: [[0] [1 2] [2 3 4]]
Let's break down the code step by step:
  1. 1.
    Package Import:
    package main
    import (
    "fmt"
    "slices"
    )
    • The main package is the entry point for the executable.
    • It imports the "fmt" package for formatted I/O and a custom "slices" package (presumably defined elsewhere) for a function called Equal.
  2. 2.
    Main Function:
    func main() {
    // ...
    }
    • The main function is the starting point of the program.
  3. 3.
    Slice Declaration and Initialization:
    var s []string
    fmt.Println("uninit:", s, s == nil, len(s) == 0)
    s = make([]string, 3)
    fmt.Println("emp:", s, "len:", len(s), "cap:", cap(s))
    • Declares an uninitialized slice s and prints its properties.
    • Initializes s with a length of 3 using make, and prints the slice along with its length and capacity.
  4. 4.
    Slice Modification:
    s[0] = "a"
    s[1] = "b"
    s[2] = "c"
    fmt.Println("set:", s)
    fmt.Println("get:", s[2])
    fmt.Println("len:", len(s))
    • Sets values in the slice s, prints the modified slice, retrieves and prints an element, and prints the length of the slice.
  5. 5.
    Slice Appending:
    s = append(s, "d")
    s = append(s, "e", "f")
    fmt.Println("apd:", s)
    • Appends elements "d", "e", and "f" to the slice s using the append function, and prints the resulting slice.
  6. 6.
    Slice Copying:
    c := make([]string, len(s))
    copy(c, s)
    fmt.Println("cpy:", c)
    • Creates a new slice c with the same length as s using make.
    • Copies the elements of s to c using the copy function and prints the copied slice.
  7. 7.
    Slice Slicing:
    l := s[2:5]
    fmt.Println("sl1:", l)
    l = s[:5]
    fmt.Println("sl2:", l)
    l = s[2:]
    fmt.Println("sl3:", l)
    • Demonstrates various ways to create sub-slices of the original slice s and prints the results.
  8. 8.
    Slice Declaration and Initialization with Literal Values:
    t := []string{"g", "h", "i"}
    fmt.Println("dcl:", t)
    • Declares and initializes a slice t with literal values "g", "h", and "i", and prints the slice.
  9. 9.
    Custom Slice Equality Check:
    t2 := []string{"g", "h", "i"}
    if slices.Equal(t, t2) {
    fmt.Println("t == t2")
    }
    • Uses a custom function Equal from the "slices" package to check if slices t and t2 are equal and prints a message accordingly.
  10. 10.
    Two-Dimensional Slice:
    twoD := make([][]int, 3)
    // ... (nested loop to initialize values)
    fmt.Println("2d: ", twoD)
    • Creates a two-dimensional slice twoD with three inner slices.
    • Initializes values in the two-dimensional slice using a nested loop.
    • Prints the two-dimensional slice.
This tutorial covers fundamental concepts related to slices in Go, including creation, modification, appending, copying, slicing, and working with two-dimensional slices. The custom Equal function demonstrates how you can extend functionality by creating your own utility functions.
Last modified 1mo ago