Successfully reported this slideshow.
Your SlideShare is downloading. ×

Jaap Groeneveld - Golang Naming and Structure

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 13 Ad

More Related Content

Recently uploaded (20)

Advertisement

Jaap Groeneveld - Golang Naming and Structure

  1. 1. GoNamingandStructure JaapGroeneveld-jGroeneveld.de
  2. 2. Agenda AverybriefintroductiontoGo NamingConventions ApplicationStructure Literature 2
  3. 3. Averybriefintroduction Goisaminimallanguage Goisacompiledlanguage Gohasadedicatedconcurrencyconcept (Goroutines,channels,select) Goisnotobjectoriented Godoesnotuseexceptionsforerrors 3
  4. 4. Naming-Variables UsecamelCasenotsnake_case Singlelettervariablesforindexes for i:=0; i < 10; i++ {} Shortanddescriptivenames var count int var customer Customer Usethescopeofthevariableasyourguide.Thefartherawayfromdeclarationyou useit,thelongerandclearerthenameshouldbe 4
  5. 5. Abbreviations Repeatedlettersforcollections var tt []*Thing (prefer things whenscopeis bigger) Singleletterinsideloop for i, t := range tt {} (preferthingwhenscopeis bigger) Commonabbreviationsareused decodeJSON , customer.ID Someabbrevationsarecommonthroughoutgocode r *http.Request , w io.Writer , w http.ResponseWriter , err error , ... 5
  6. 6. Naming-Interfaces Nameoftheinterfaceshoulddescribeitsfunctionality CustomerStore Ifinterfacehasonlyonefunction,append"-er" type Stringer interface{ String() string } 6
  7. 7. Naming-Packages Packagesnamesdescribetheirpurpose Itʼsveryeasytoseewhatapackagedoesbylookingatthename Namesaregenerallyshort archive cmd crypto errors math Whennecessary,useadescriptiveparentpackageandseveralchildren implementingthefunctionality encoding/base64 encoding/csv encoding/json Avoidcatchallpackages util helpers Avoidapackage-levelfunctionnamethatrepeatsthepackagename log.Info() vs log.LogInfo() 7
  8. 8. Naming-Files Insideapackageseparatecodeintologicalconcerns. Ifthepackagedealswithmultipletypes,keepthelogicforeachtypeinitsownsource file: package: db orders.go suppliers.go products.go 8
  9. 9. ApplicationStructure Applicationpackageorganizationhasahugeimpactonthetestabilityand functionalityofyoursystem Whenwritinganapplicationyourgoalshouldbetowritecodethatiseasyto understand,easytorefactor,andeasyforsomeoneelsetomaintain. Thereisnotoneapplicationstructure. Oneidea:Separatedomain,servicesanddependencies(adapters).benbjohnson 9
  10. 10. ApplicationStructureexample Dependenciesandservicesshouldbeinseparatepackagesforeasiertesting db , someapi ... Domaintypesandinterfacestooperateonthemareinroot Dependenciesandservicesimplementdomaininterfaces Binaries(CLI/Server...)areinseparatepackagesasseparatemains cmd andglue domainandservicestogether. Thisincreasescohesion,reducescouplingandpreventscirculardependencies 10
  11. 11. 11
  12. 12. ApplicationStructureexample - myapp - cmd - cli - main.go // glues services and dependencies together for the cli - server - main.go // glues services and dependencies together for the server - api // contains our api (using domain interfaces and models) - db // dependency to connect and manage data in db - someapi // dependency to connect to "someapi" - users // contains services for users - users.go // domain models and interfaces regarding users - ... // further domain models and interfaces 12
  13. 13. GeneralGoliterature EffectiveGoTheguide GobyExamplehands-onintroductionusingannotatedexampleprograms Gogo-toguideGotchas,cheatsheets,blueprintsetc Learngowithtests awesome-golistoflibraries go-kitexampleprojectsformicroservices UberStyleguide APIDesignBestPractices 13

×