-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
129 lines (95 loc) · 2.93 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"math"
"unsafe"
)
// Part 1: Are these structs comparable?
type s1 struct {
n int
b bool
}
type s2 struct {
r []rune
}
type s3 struct {
r [3]rune
}
// Part 2: Fun with empty structs
type emptyStruct struct{}
type metaEmptyStruct struct {
A struct{}
B struct{}
}
type sliceOfNothings []struct{}
// *** Task 1: A Set type ***
// A map with struct{} values acts like a set. The keys are the
// elements of the set, and inserting a key/value pair whose key
// already exists changes nothing. (The value is always empty anyway.)
type IntSet map[int]struct{}
func testSetType() {
set := IntSet{}
// Insert a value into the set
// This is a shortcut, to avoid typing struct{}{} all the time
var el = struct{}{}
set[5] = el
set[28] = el
set[12] = el
set[12] = el // adding twice does not change the set
fmt.Println("Set after adding 5, 28, 12, and 12:", set)
// Test if a value exists in the set
if _, ok := set[5]; ok {
fmt.Println("5 exists")
}
// Get all values from the set
fmt.Print("'set' consists of: ")
for element := range set {
fmt.Print(element, ",")
}
fmt.Println()
}
// Task 2: An iterator over ints
// This is taken from Brad Fitzpatrick's iter package.
// https://godoc.org/github.com/bradfitz/iter
// iter returns a slice of empty structs of length n.
func iter(n int) []struct{} {
return make([]struct{}, n)
}
// Now we can run a range loop over the return value of iter().
func iterateFrom1ToN(n int) {
for i := range iter(n) {
fmt.Print(i, ",")
}
fmt.Println()
}
func main() {
fmt.Println("\n*** Part 1: Are these structs comparable? ***\n")
s11 := s1{n: 4, b: true}
s12 := s1{n: 4, b: true}
fmt.Println("s1 is comparable. Result:", s11 == s12)
_ = s2{r: []rune{'a', 'b', '🎵'}}
_ = s2{r: []rune{'a', 'b', '🎶'}}
fmt.Println("s2 is not comparable, as slices are not comparable.")
s31 := s3{r: [3]rune{'a', 'b', '🎵'}}
s32 := s3{r: [3]rune{'a', 'b', '🎶'}}
fmt.Println("s3 is comparable, as arrays are comparable. Result:", s31 == s32)
fmt.Println("\n*** Part 2: Empty struct basics ***\n")
es := emptyStruct{}
mes := metaEmptyStruct{}
fmt.Println("Size of es:", unsafe.Sizeof(es))
fmt.Println("Size of mes:", unsafe.Sizeof(mes))
sOfN := make(sliceOfNothings, math.MaxInt64) // try this with []int
// We cannot use unsafe.Sizeof() on sOfN, since Sizeof() does not
// follow pointers and therefore only shows the size of the slice
// header, which is always the same.
// Don't try to print out sOfN, it will try printing "{}" MaxInt64
// times! Let's print out a shorter version instead.
// Note that len() and cap() report values > 0, but this does not
// mean that any memory has been allocated for the struct{}{} elements.
sOfN = make(sliceOfNothings, 7)
fmt.Println(sOfN, "len:", len(sOfN), "cap:", cap(sOfN))
fmt.Println("\n*** Part 2, task 1: Set type ***\n")
testSetType()
fmt.Println("\n*** Part 2, task 2: iterator ***\n")
iterateFrom1ToN(7)
}