Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

The Best Feature of Go – A 5 Year Retrospective

1,231 views

Published on

After shipping Go code to production across 3 companies and 5 years, one feature of Go stands out as really special to me. That's what I talked about at Tokopedia Tech-a-Break GoJakarta Meetup.

Published in: Technology
  • Be the first to comment

The Best Feature of Go – A 5 Year Retrospective

  1. 1. THE BEST FEATURE OF GO A 5-YEAR RETROSPECTIVE
  2. 2. GO & I ▸ Programming since the ‘90s ▸ Former C, C++, PHP, Python, JavaScript, Java programmer ▸ Switched to Go in 2012 ▸ Handful of large Go implementations across 2 companies ▸ Have my own shortlist of favorite Go features over the years ▸ One of those features is exceptional
  3. 3. NOTABLE FEATURES
  4. 4. COMPILER & SYNTAX ▸ Avoids historically error prone constructs ▸ Highly assistive error messages ▸ Focus on intention rather than expression ▸ If it compiles, it mostly works ▸ Very high performance with low effort
  5. 5. SMALL FEATURE SET ▸ A tad bigger than C ▸ Orthogonal, Minimal ▸ Easy to Learn ▸ Easy to Remember ▸ Minimized Personal Feature Subsets
  6. 6. STANDARDIZED FORMATTING ▸ 10 x improvement in readability ▸ Improved collaboration between team members ▸ Improved collaboration across teams ▸ More easily accessible Open Source code
  7. 7. SENSIBLE UNICODE & STRINGS ▸ UTF-8 encoded strings ▸ Compatible []byte and string memory layout ▸ Minimized copying between buffers and strings ▸ Rich set of string manipulators available
  8. 8. GOROUTINES & CHANNELS ▸ Declarative Parallel Programming ▸ Based on CSP Principles ▸ Straight-forward procedural logic — no callback mess ▸ Any function/method can be invoked asynchronously
  9. 9. SELECT STATEMENT ▸ Go’s tour de force in parallel programming ▸ Simple, declarative expression of event synchronization ▸ Makes previously complicated flows trivial to implement ▸ Timeouts ▸ Cancellation ▸ Back-pressure handling ▸ Combinations of the above
  10. 10. CAPABILITY GO CHANGED MY PROGRAMMING
  11. 11. PERFORMANCE AND COMPLEXITY WITH CONFIDENCE ▸ First production system in Go: a Multiplayer Game Engine ▸ Made > $1000,000 in a year ▸ Also, a rate limiter, a reverse proxy, a micro-service simulator, deployment orchestrator, cluster auto-scaler… ▸ … even a bespoke datastore!
  12. 12. PROGRAMMER ONE FEATURE CHANGED ME AS A
  13. 13. THE BEST FEATURE OF GO ▸ Acts as a stand-in for the user ▸ Improves documentation ▸ Guides program design ▸ Aids Refactoring & Debugging ▸ Finds unnecessary/dysfunctional code ▸ Gives rapid feedback on performance characteristics Can you guess what it is?
  14. 14. GO TEST
  15. 15. GO TEST IN DESIGN PHASE USER SIMULATION Use package pkg_test to get “3rd Party” view ▸ Allows accessing your package like an end-user ▸ Helps the provider switch mind to that of a consumer ▸ Helps in deciding exported symbols ▸ Improves API Design
  16. 16. GO TEST IN DESIGN PHASE DOCUMENTATION ▸ Write testable, runnable Examples that show up in the documentation ▸ Document methods: func ExampleStruct_Method() {…} ▸ Document functions: func ExampleFunction() {…} ▸ Document use cases: func Example_useCase() {…}
  17. 17. GO TEST IN DESIGN PHASE PROGRAM DESIGN ▸ Monolithic do-it-all functions are hard to test ▸ The need to test improves modularity, functional cohesion ▸ Sample Strategy: 1. Implement business logic as native APIs with native data — test entire business logic 2. Implement data load/store from native to database — test data validation and access 3. Implement remoting (HTTP etc.) APIs — test request interpretation and response serialization
  18. 18. GO TEST IN IMPLEMENTATION PHASE REFACTORING ▸ Table driven testing helps in basic I/O validation ▸ Top-level tests for checking various scenarios ▸ Subtests help in isolating granular functionality ▸ Non-compilable tests indicate changes in package interface ▸ Failing tests indicate changes in functional behaviours
  19. 19. GO TEST IN IMPLEMENTATION PHASE DEBUGGING ▸ Parallel tests with race detector for unsafe concurrent data access ▸ Optional long running tests for deeper verification ▸ Test cases can be complex enough to act as simulators. 
 
 E.g. the graph here is from a 1- million iteration sim of smartcb Error Rate Learned Rate SmartCB Regular CB
  20. 20. GO TEST IN IMPLEMENTATION PHASE FINDING USELESS CODE ▸ $ go test -coverprofile to find which lines were executed by the test ▸ Untouched lines => ▸ The tests don’t check the logic for which the code is written, or ▸ The code is untestable because it’s dead or represents a logically impossible scenario
  21. 21. GO TEST IN OPTIMIZATION PHASE BENCHMARKING ▸ $ go test -bench to see how fast functions/methods are ▸ $ go test -bench -cpu to observe multi-core scalability ▸ $ go test -bench -cpuprofile to see what parts of the code are slow ▸ $ go test -bench -memprofile to see what parts of the code eat up memory ▸ Run go test -bench before every commit to find performance regressions
  22. 22. GO TEST IS LIKE A PROGRAMMER’S ASSISTANT. AN IRONMAN’S JARVIS. A BATMAN’S ALFRED. IT HAPPENS TO RUN UNIT TESTS TOO Yours Truly MORE THAN UNIT TESTING
  23. 23. THANK YOU TAHIR HASHMI, PT TOKOPEDIA, JAN 2018
  24. 24. FURTHER INFORMATION ▸ talks.golang.org ▸ tour.golang.org ▸ tech.t9i.in ▸ @code_martial

×