Skip to content

Commit d882fbf

Browse files
committed
oops
1 parent 6da5911 commit d882fbf

3 files changed

Lines changed: 265 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
🗓️ 02112024 2312
2+
📎
3+
4+
# go_goroutines
5+
6+
**Core Concept**: Goroutines are lightweight, independently executing functions managed by the Go runtime.
7+
8+
## Why It Matters
9+
10+
Enable concurrent programming with minimal overhead. Thousands of goroutines can run simultaneously. Core feature of Go.
11+
12+
## When to Use
13+
14+
**Use when:**
15+
- I/O-bound operations (network, file)
16+
- Independent tasks can run concurrently
17+
- Need to handle multiple requests simultaneously
18+
- Background processing
19+
20+
**Don't use when:**
21+
- Sequential operations required
22+
- Shared state without synchronization
23+
- Uncertain about race conditions
24+
25+
## vs Java Threads
26+
27+
| Java Threads | Go Goroutines |
28+
|--------------|---------------|
29+
| ~1MB stack | ~2KB stack (grows as needed) |
30+
| OS-managed | Go runtime-managed |
31+
| Heavy creation cost | Cheap creation |
32+
| Thread pools needed | Create freely |
33+
34+
```ad-danger
35+
Always ensure goroutines can exit. Use [[go_context]] for cancellation to prevent goroutine leaks.
36+
\`\`\`
37+
38+
## Trade-offs
39+
40+
**Pros**: Lightweight, easy creation, efficient scheduling
41+
**Cons**: Requires synchronization, race conditions possible, debugging harder
42+
43+
Goroutines work with [[go_context]] for cancellation and channels for communication.
44+
45+
## Quick Reference
46+
47+
```go
48+
// Start goroutine with go keyword
49+
go doWork()
50+
51+
// Anonymous function goroutine
52+
go func() {
53+
fmt.Println("Running concurrently")
54+
}()
55+
56+
// With parameters (evaluated immediately)
57+
go process(data)
58+
59+
// Proper cancellation with context
60+
func worker(ctx context.Context) {
61+
for {
62+
select {
63+
case <-ctx.Done():
64+
return // Exit when cancelled
65+
default:
66+
// do work
67+
}
68+
}
69+
}
70+
71+
ctx, cancel := context.WithCancel(context.Background())
72+
go worker(ctx)
73+
// Later: cancel() to stop goroutine
74+
```
75+
76+
## Common Patterns
77+
78+
**Wait for completion:**
79+
```go
80+
var wg sync.WaitGroup
81+
wg.Add(1)
82+
go func() {
83+
defer wg.Done()
84+
doWork()
85+
}()
86+
wg.Wait()
87+
```
88+
89+
**Fan-out (multiple workers):**
90+
```go
91+
for i := 0; i < numWorkers; i++ {
92+
go worker(i, jobs, results)
93+
}
94+
```
95+
96+
## References
97+
98+
- [Go Tour: Goroutines](https://go.dev/tour/concurrency/1)
99+
- [Effective Go: Goroutines](https://go.dev/doc/effective_go#goroutines)
100+
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
🗓️ 02112024 2250
2+
📎
3+
4+
# go_learning_plan
5+
6+
**Core Concept**: Structured path for Java Spring Boot developers learning Go, focusing on mental model shifts.
7+
8+
## Why It Matters
9+
10+
Go requires different thinking than Java. Learning in order prevents confusion and builds solid foundations.
11+
12+
## Learning Path
13+
14+
### Phase 1: Mental Model Shifts (Week 1)
15+
**Goal**: Understand how Go differs from Java fundamentally
16+
17+
1. [[go_error_handling]] - No exceptions, errors are values
18+
2. [[go_pointers]] - Explicit memory addresses, not Java references
19+
3. [[go_interfaces]] - Implicit implementation (duck typing)
20+
4. [[go_defer]] - Resource cleanup without try-finally
21+
22+
**Practice**: Rewrite simple Java methods in Go
23+
24+
### Phase 2: Core Patterns (Week 2)
25+
**Goal**: Learn Go idioms and common patterns
26+
27+
5. [[go_type_assertions]] - Working with interface{}
28+
6. [[go_context]] - Request lifecycle management
29+
7. [[go_dependency_injection]] - Constructor injection, no @Autowired
30+
31+
**Next to learn**: Struct methods, goroutines, channels
32+
33+
**Practice**: Build a simple HTTP API with clean architecture
34+
35+
### Phase 3: Concurrency (Week 3+)
36+
**Goal**: Master Go's concurrency primitives
37+
38+
- Goroutines - Lightweight threads
39+
- Channels - Communication between goroutines
40+
- Select - Multiplexing channels
41+
- Sync primitives - Mutexes and WaitGroups
42+
43+
**Practice**: Build concurrent data processor
44+
45+
### Phase 4: Production Readiness (Week 4+)
46+
**Goal**: Write production-grade Go code
47+
48+
- Testing - Table-driven tests
49+
- Error wrapping - Error chains with %w
50+
- Graceful shutdown - Clean service termination
51+
- Project structure - Organizing real applications
52+
53+
**Practice**: Production-ready microservice
54+
55+
## Key Mental Shifts
56+
57+
| Java Concept | Go Equivalent | Note |
58+
|--------------|---------------|------|
59+
| try/catch | if err != nil | Explicit |
60+
| @Autowired | Constructor params | Manual |
61+
| implements | Implicit | Duck typing |
62+
| synchronized | Mutexes/Channels | Explicit |
63+
| ThreadLocal | context.Context | Passed explicitly |
64+
65+
## Study Tips
66+
67+
- **Read standard library code** - Best Go examples
68+
- **Run `go fmt`** - Learn idiomatic style
69+
- **Write tests first** - Forces good design
70+
- **Keep functions small** - 20-30 lines max
71+
72+
## Trade-offs
73+
74+
**Java strengths**: Rich frameworks, strong tooling, massive ecosystem
75+
**Go strengths**: Simplicity, fast compilation, built-in concurrency, single binary deployment
76+
77+
This learning plan focuses on concepts that build on [[go_interfaces]] and [[go_error_handling]] as foundational patterns.
78+
79+
## References
80+
81+
- [Tour of Go](https://go.dev/tour/)
82+
- [Effective Go](https://go.dev/doc/effective_go)
83+
- [Go by Example](https://gobyexample.com/)
84+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
🗓️ 02112024 2310
2+
📎
3+
4+
# go_pass_by_value
5+
6+
**Core Concept**: Go always passes arguments by value - copies are made of all parameters, including pointers.
7+
8+
## Why It Matters
9+
10+
Explains why modifying parameters doesn't affect original values unless using pointers. Critical for understanding [[go_pointers]].
11+
12+
## When to Use
13+
14+
This is not a pattern to "use" - it's how Go always works. You choose whether to pass values or pointers based on whether you need mutation.
15+
16+
## Key Distinction
17+
18+
**Pass value:**
19+
```go
20+
func increment(x int) {
21+
x = x + 1 // Modifies copy, not original
22+
}
23+
24+
a := 5
25+
increment(a)
26+
fmt.Println(a) // Still 5
27+
```
28+
29+
**Pass pointer (pointer value is copied, but points to same address):**
30+
```go
31+
func increment(x *int) {
32+
*x = *x + 1 // Dereferences and modifies original
33+
}
34+
35+
b := 5
36+
increment(&b)
37+
fmt.Println(b) // Now 6
38+
```
39+
40+
## vs Java References
41+
42+
| Java | Go |
43+
|------|-----|
44+
| Objects passed by reference | Everything passed by value |
45+
| Primitives passed by value | Pointers passed by value (address copied) |
46+
| No pointer syntax | Explicit `&` and `*` |
47+
48+
## Trade-offs
49+
50+
**Pros**: Predictable, no hidden side effects, explicit mutation
51+
**Cons**: Large structs can be expensive to copy
52+
53+
This is why [[go_pointers]] are necessary - to pass the address (by value) instead of copying large data.
54+
55+
## Quick Reference
56+
57+
```go
58+
// Value passed - copy made
59+
func modifyValue(s MyStruct) {
60+
s.Field = "changed" // Changes copy only
61+
}
62+
63+
// Pointer passed - address copied, data accessible
64+
func modifyPointer(s *MyStruct) {
65+
s.Field = "changed" // Changes original
66+
}
67+
68+
// Even the pointer itself is copied!
69+
func reassignPointer(p *int) {
70+
newVal := 100
71+
p = &newVal // Only changes local copy of pointer
72+
}
73+
```
74+
75+
**Key insight**: When you pass a pointer, the address is copied, but both the original and copy point to the same memory location.
76+
77+
## References
78+
79+
- [Go FAQ: Pass by Value](https://go.dev/doc/faq#pass_by_value)
80+
- [Effective Go: Pointers vs Values](https://go.dev/doc/effective_go#pointers_vs_values)
81+

0 commit comments

Comments
 (0)