Concept Explanation
Performance in Go often starts with writing the simplest thing that works, then removing waste where it matters. In this lesson, you will build a small example, inspect where extra work happens, and make one focused improvement without making the code harder to read. The goal is to practice choosing efficient data flow, avoiding unnecessary allocations when possible, and keeping functions small enough to reason about.
Where to Put the Code
- Define color and position variables at the top.
- Create shape drawing or placement logic in the middle.
- Render output (print, canvas, SVG, or styled block) at the end.
Command Reference
- Run the code, then note which part repeats work and which part stays constant.
- Measure one small change, such as preallocating a slice or reusing a value, and compare the result.
- Keep package structure simple so performance changes stay easy to review.
- Write down one reason your faster version is still readable.
Step-by-step Guide
- Run the starter code first so you know the baseline behavior.
- Identify one place where the program does more work than necessary.
- Make one improvement only, then rerun the program and compare the output.
- Explain in one sentence why your change helps performance.
- Finish with a quick check: correct result, clear names, and no unnecessary complexity.
Practice Exercises
- Rewrite the example so it builds a larger slice efficiently without changing the final result.
- Create a second version that solves the same task in a simpler but slightly less optimized way, then compare the trade-offs.
- Change the example to work with strings instead of integers and keep the code efficient.
Coding Challenges
- Process a larger input set and keep the code easy to follow.
- Find a performance improvement that does not rely on clever tricks or confusing code.
Mini Practice Tasks
- Rename one variable to make its purpose clearer.
- Add a short comment explaining the performance choice you made.
- Print the length and capacity of the slice to confirm what the program is doing.
Common Mistake
Mixing x and y axes or using wrong coordinate origin causes shapes to appear in unexpected places.
Real-life Mini Challenge
Draw one square, one triangle, and one circle, then move X marker 2 steps right and 1 step down.