-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_usage_test.go
75 lines (59 loc) · 1.98 KB
/
example_usage_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package tttuples_test
import (
"fmt"
"github.com/YongJieYongJie/tttuples/atuple"
)
func Example_usage() {
// import "github.com/YongJieYongJie/tttuples/atuple"
// Save as otherwise noted below, this example applies to stuple as well.
fmt.Println("Basic declaration and initialization:")
arrayTuple := atuple.Pack3(123, "abc", "def")
fmt.Printf("%v\n", arrayTuple)
fmt.Printf("%#v\n", arrayTuple)
fmt.Println("\nUnpacking tuple items:")
item1, item2, item3 := arrayTuple.Unpack()
fmt.Println(item1)
fmt.Println(item2)
fmt.Println(item3)
fmt.Println("\nUnpacking tuple items into function arguments:")
MultiArg := func(a1 int, a2 string, a3 string) { fmt.Printf("MultiArg() called with %v, %v, and %v\n", a1, a2, a3) }
Variadic := func(a1 ...any) { fmt.Printf("Variadic() called with %v\n", a1) }
Mixed := func(a1 int, a2 ...string) { fmt.Printf("Mixed() called with %v, and %v\n", a1, a2) }
MultiArg(arrayTuple.Unpack())
Variadic(arrayTuple.Unpack())
Mixed(arrayTuple.Unpack())
fmt.Println("\nRange loop:")
for _, item := range arrayTuple { // Note: for stuple, we need to call .ToArray() on the tuple object first.
fmt.Println(item)
}
fmt.Println("\nDeclaring and initializing slice of tuples:")
arrayTuples := []atuple.Packed3[int, string, float64]{
{1, "b", 3.3},
{4, "e", 6.6},
}
fmt.Printf("%v\n", arrayTuples)
fmt.Printf("%#v\n", arrayTuples)
// Output:
// Basic declaration and initialization:
// [123 abc def]
// atuple.Packed3[int,string,string]{123, "abc", "def"}
//
// Unpacking tuple items:
// 123
// abc
// def
//
// Unpacking tuple items into function arguments:
// MultiArg() called with 123, abc, and def
// Variadic() called with [123 abc def]
// Mixed() called with 123, and [abc def]
//
// Range loop:
// 123
// abc
// def
//
// Declaring and initializing slice of tuples:
// [[1 b 3.3] [4 e 6.6]]
// []atuple.Packed3[int,string,float64]{atuple.Packed3[int,string,float64]{1, "b", 3.3}, atuple.Packed3[int,string,float64]{4, "e", 6.6}}
}