Skip to content

Commit 1e1cbf2

Browse files
committed
Updated Closure and channels example.
1 parent 81e2a42 commit 1e1cbf2

File tree

3 files changed

+66
-44
lines changed

3 files changed

+66
-44
lines changed

README.md

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,56 @@
22

33
The goal is to compile hidden gems and off-the-beaten path Go features and patterns. This is not meant to be a library or reference implementation.
44

5+
## Gems and Off-the-beaten Path
56

6-
* [add_two_numbers](./add_two_numbers)
7-
* [main.go](./add_two_numbers/main.go)
8-
* [binary_uint8_reversal](./binary_uint8_reversal)
9-
* [main.go](./binary_uint8_reversal/main.go)
10-
* [caesar_cipher](./caesar_cipher)
11-
* [main.go](./caesar_cipher/main.go)
127
* [composite_literals](./composite_literals)
138

149
It is important to understand what **composite literals** mean in Go through some interesting use-cases
10+
11+
* [for_with_label](./for_with_label)
12+
13+
For loops can have break and continue statements with **labels**. This is a useful but less well-known feature.
14+
15+
* [runes_bytes_loops](./runes_bytes_loops)
16+
17+
A must-understand for serious programmers. Simple program demonstrating tricky [concepts](https://blog.golang.org/strings). Do you know that 'a' is a int32, str[0] is a uint8, the value of position 0 in a range loop is a rune?
18+
19+
* [slice_gotchas](./slice_gotchas)
20+
21+
Do you know that this does not cause an out of bounds error?
22+
23+
```go
24+
c := make([]int, 3)
25+
fmt.Println(c[len(c):len(c)])
26+
```
27+
28+
* [slice_tricks](./slice_tricks)
1529

16-
* [main.go](./composite_literals/main.go)
30+
Implementation of simple functions that demonstrate interesting slice operations such as [**extending, expanding and inserting**](https://github.com/golang/go/wiki/SliceTricks) elements.
31+
32+
* [variadic_functions](./variadic_functions)
33+
34+
Variadic functions in Go are certainly off-the-beaten path. One hidden gem is **interface{} variadic functions**
35+
36+
## Classic Problems
37+
38+
* [add_two_numbers](./add_two_numbers)
39+
* [binary_uint8_reversal](./binary_uint8_reversal)
40+
* [caesar_cipher](./caesar_cipher)
1741
* [eratosthenes](./eratosthenes)
1842

1943
A Classic problem but I wanted to dig deeper into the number theory behind it. I suggest reading
2044
[Why in Sieve of Erastothenes of 𝑁 number you need to check and cross out numbers up to 𝑁‾‾√? How it's proved?](https://math.stackexchange.com/questions/58799/why-in-sieve-of-erastothenes-of-n-number-you-need-to-check-and-cross-out-numbe)
2145

22-
* [main.go](./eratosthenes/main.go)
23-
* [for_with_label](./for_with_label)
24-
25-
For loops can have break and continue statements with **labels**. This is a useful but less well-known feature.
26-
27-
* [main.go](./for_with_label/main.go)
2846
* [go_routines_gotchas](./go_routines_gotchas)
29-
* [main.go](./go_routines_gotchas/main.go)
3047
* [is_rotation](./is_rotation)
31-
* [main.go](./is_rotation/main.go)
3248
* [longest_substring](./longest_substring)
33-
* [main.go](./longest_substring/main.go)
3449
* [median_sorted_arrays](./median_sorted_arrays)
35-
* [main.go](./median_sorted_arrays/main.go)
3650
* [one_away](./one_away)
37-
* [main.go](./one_away/main.go)
3851
* [palindromes](./palindromes)
39-
* [main.go](./palindromes/main.go)
4052
* [permutations](./permutations)
41-
* [main.go](./permutations/main.go)
42-
* [runes_bytes_loops](./runes_bytes_loops)
43-
44-
A must-understand for serious programmers. Simple program demonstrating (sometimes tricky) concepts found in https://blog.golang.org/strings. Do you know that 'a' is a int32, str[0] is a uint8, the value of position 0 in a range loop is a rune?
45-
46-
47-
* [main.go](./runes_bytes_loops/main.go)
48-
* [slice_tricks](./slice_tricks)
49-
50-
Implementation of https://github.com/golang/go/wiki/SliceTricks. Simple functions that demonstrate interesting slice operations such as **extending, expanding and inserting** elements.
51-
52-
* [main.go](./slice_tricks/main.go)
5353
* [two_sum](./two_sum)
54-
* [main.go](./two_sum/main.go)
5554
* [unique_chars](./unique_chars)
56-
* [main.go](./unique_chars/main.go)
5755
* [urlify](./urlify)
58-
* [main.go](./urlify/main.go)
59-
* [variadic_functions](./variadic_functions)
60-
61-
Variadic functions in Go are certainly off-the-beaten path. One hidden gem is **interface{} variadic functions**
56+
6257

63-
* [main.go](./variadic_functions/main.go)

embedded_structs/main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type topLevel struct {
6+
level2
7+
}
8+
9+
type level2 struct {
10+
string2 string
11+
printString func(string) bool
12+
level3
13+
}
14+
15+
type level3 struct {
16+
string3 string
17+
}
18+
19+
func main() {
20+
t := new(topLevel)
21+
t.printString = func(a string) bool {
22+
fmt.Println(a)
23+
return true
24+
}
25+
t.string3 = "level3"
26+
}

slice_gotchas/main.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ func main() {
1414
b := "example"
1515
fmt.Printf("b is: %q \n", b)
1616
fmt.Printf("b[len(b):] is not out of range: %q \n", b[len(b):])
17-
a := make([]int, 3, 5)
18-
fmt.Println("a is: ", a)
19-
fmt.Println("a[5:5] is not out of bounds: ", a[5:5])
20-
fmt.Println("Because it is a slice of int and cap is: ", cap(a))
21-
fmt.Println("But a[5:] is out of bounds since a missing high index defaults to the length ")
22-
_ = a[5:]
17+
c := make([]int, 3)
18+
fmt.Println("c[len(c):len(c)] is not out of bounds: ", c[len(c):len(c)])
19+
d := make([]int, 3, 5)
20+
fmt.Println("d is: ", d)
21+
fmt.Println("d[5:5] is not out of bounds: ", d[5:5])
22+
fmt.Println("Because it is d slice of int and cap is: ", cap(d))
23+
fmt.Println("But d[5:] is out of bounds since d missing high index defaults to the length ")
24+
_ = d[5:]
2325
}

0 commit comments

Comments
 (0)