github.com/enetx/g@v1.0.80/examples/slices/slices_unpack.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/enetx/g" 7 ) 8 9 func main() { 10 // Example 1: Using the Split method 11 account := g.String("e@mail.com:password") 12 13 // Define variables to store the result of splitting 14 var mail, password g.String 15 16 // Split the account string by ":" and unpack the result into mail and password variables 17 account.Split(":").Collect().Unpack(&mail, &password) 18 19 // Print the result 20 fmt.Println(mail, password) 21 22 // Example 2: Using the Unpack method with a slice 23 numbers := g.Slice[int]{1, 2, 3, 4, 5} 24 25 // Define variables to store the unpacked values 26 var a, b, c int 27 28 // Unpack the first three elements of the numbers slice into variables a, b, and c 29 numbers.Unpack(&a, &b, &c) 30 31 // Print the result 32 fmt.Println(a, b, c) 33 34 // Example 3: Using the Unpack method with a slice of custom struct 35 // Define a custom struct 36 type character struct { 37 Name string 38 Age int 39 } 40 41 // Create a slice of custom struct 42 characters := g.Slice[character]{ 43 {Name: "Tom", Age: 6}, 44 {Name: "Jerry", Age: 2}, 45 } 46 47 // Define variables to store the unpacked values 48 var tom, jerry character 49 50 // Unpack the first two elements of the characters slice into variables tom and jerry 51 characters.Unpack(&tom, &jerry) 52 53 // Print the names of tom and jerry 54 fmt.Println(tom.Name, jerry.Name) 55 }