New 42 day free trial
Smarty

Go naming conventions

Smarty header pin graphic
Michael Whatcott
Michael Whatcott
 • 
October 18, 2018
Tags

It's been said that naming is one of the two hardest problems in computer science, along with cache invalidation and 'off-by-one' errors. (See what I did there?) Do you ever find yourself wondering what policies and practices you could adopt to make your life easier when reading code you wrote months ago? Or maybe you're up at night wishing you knew how to write code in such a way as to maximize adoption and convenience for your users? Well, look no further because we've anticipated the need, solved the problem, and now we're sharing our knowledge and wisdom at no charge, all out of the goodness of our hearts in this comprehensive, totally no-nonsense (nudge, nudge, wink, wink) style guide of Go naming conventions.

What you’re about to read might actually be helpful at some point, but we're not betting on it. Don't try this at home...actually, do try this at home--but maybe don't try it at work.

Table of contents

  1. Export local variable names
  2. Export local const names
  3. Export input argument names
  4. Export output arguments names
  5. Export reciever names
  6. Use single-character receiver names
  7. Use single-letter argument names
  8. Use double-letter names when you run out of single-letter
  9. Actually, use a generic receiver name like 'this',
  10. For added emphasis, use extended unicode characters for receiver names
  11. Always define import aliases
  12. Always export all imports
  13. Use single-letter (exported) import aliases
  14. In the spirit of #8 (above), use double-letter alias names when necessary

1: Export local variable names

package main

import "fmt"

func main() { Message := "Always export local variable names" fmt.Println(Message) }

https://go.dev/play/p/8WVCvJpoa59

2: Export local const names

package main

import "fmt"

func main() { const Message = "Always export constants defined in functions" fmt.Println(Message) }

https://go.dev/play/p/-0yZhHVNOOs

3: Export input argument names

package main

import "fmt"

func main() { Print("Always export input argument names") }

func Print(Message string) { fmt.Println(Message) }

https://go.dev/play/p/utRBMOMQNgj

4: Export output arguments names

package main

import "fmt"

func main() { Print("Always export output argument names") }

func Print(Message string) (N int, Err error) { return fmt.Println(Message) }

https://go.dev/play/p/n5cJhLDKNWk

5: Export reciever names

package main

import "fmt"

func main() { new(Printer).Print("Always export receiver names") }

type Printer struct{}

func (Printer *Printer) Print(Message string) (N int, Err error) { return fmt.Println(Message) }

https://go.dev/play/p/jEN-zkrjxdT

6: Use single-character receiver names

package main

import "fmt"

func main() { new(Printer).Print( "Use only the first letter of a type as the receiver for its methods (oh, wait...), " + "and (per tip #5) make sure the receiver is exported") }

type Printer struct{}

func (P *Printer) Print(Message string) (N int, Err error) { return fmt.Println(Message) }

https://go.dev/play/p/0OqQLnPPcVd

7: Use single-letter argument names

package main

import "fmt"

func main() { new(Printer).Print("Use single-letter variables whenever possible") }

type Printer struct{}

func (P *Printer) Print(M string) (N int, E error) { return fmt.Println(M) }

https://go.dev/play/p/Q1jgH_6h2kT

8: Use double-letter names when you run out of single-letter names

package main

import "fmt"

func main() { new(Printer).Print("Use double-letter variables when you run out of single-letter variables") }

type Printer struct{}

func (P *Printer) Print(NN string) (N int, E error) { return fmt.Println(NN) }

https://go.dev/play/p/k3p9Hf49-20

9: Actually, use a generic receiver name like 'this', 'self', or 'me'

package main

import "fmt"

func main() { new(Printer).Print("On second thought, use a generic receiver name like 'this', 'self', or 'me'.") }

type Printer struct{}

func (this *Printer) Print(NN string) (N int, E error) { return fmt.Println(NN) }

https://go.dev/play/p/mSMZRqUy4qw

10: For added emphasis, use extended unicode characters for receiver names

package main

import "fmt"

func main() { new(Printer).Print("See what I did here? ;)") }

type Printer struct{}

func (𝕥𝕙𝕚𝕤 *Printer) Print(NN string) (N int, E error) { return fmt.Println(NN) }

https://go.dev/play/p/VPpSDOZYYjT

11: Always define import aliases

package main

import fmt "fmt"

func main() { fmt.Println("Always define import aliases") }

https://go.dev/play/p/zCOnEoNtAf4

12: Always export all imports

package main

import Fmt "fmt"

func main() { Fmt.Println("Always export all imports") }

https://go.dev/play/p/_fEPiypASub

13: Use single-letter (exported) import aliases

package main

import F "fmt"

func main() { F.Println("Use single-letter (exported) import aliases") }

https://go.dev/play/p/e8JQAlSKpnZ

14: In the spirit of #8 (above), use double-letter alias names when necessary

package main

import ( F "flag" FF "fmt" )

func main() { F.Parse() FF.Println("Use double-letter alias names when necessary") }

https://go.dev/play/p/U0ac86PHUsb

Subscribe to our blog!
Learn more about RSS feeds here.
rss feed icon
Subscribe Now
Read our recent posts
Inside Smarty® - Irina O'hara
Arrow Icon
Irina O'Hara is one of our uniquely clever, expert frontend developers. She’s immensely talented and has had a vital impact on our website redesign. When it came time to spotlight her, Irina was a joy to sit down with and get to know a little better. To get to the basics, she writes code and creates awesome websites, and she’s darn good at both. BackgroundIrina was born and raised in St. Petersburg, Russia. However, she wasn't born a development expert and had other aspirations from the start.
How I reduced my returned mail from 27% to 1% using address autocomplete
Arrow Icon
The following is based on a true story. Some of the names and relationships have been changed to protect the anonymity of individuals and companies. However, the numbers are 100% accurate. In 2023, I wanted to mail some really fancy cards to 165 businesses. I collected their addresses by asking for them or finding them in their online listing and collected them all in a neat little row. Then, I went a step further and ran these addresses through Smarty's bulk address validation tool. Everything was set and perfect.
The ROI of accurate healthcare address validation: Stop hemorrhaging red on your financial statements
Arrow Icon
In healthcare, the havoc an inaccurate address can wreak on your financial results is significant in more ways than one, and the boost in overall profitability from maintaining a clean address database is equally worth noting. Accurate healthcare address validation improves operational efficiency, patient engagement, and compliance and builds revenue to heights that couldn’t be met without it. Here’s what we’ll be covering:Healthcare address validation pros and consCon: Increased claim denials and organizational costsPro: Reduced claim denials and reprocessing costsCon: Increasing patient match error ratesPro: Improved patient matching and data qualityCon: Complicated billing and collections processesPro: Streamlined billing and collections capabilitiesCon: Exposure to legal liabilitiesPro: Enhanced regulatory compliance and risk aversionCon: Misplaced market strategyPro: Data-driven decision-making and market insightsEpilogue: Avoiding the pain (see our summarized financial savings)Healthcare address validation pros and consThere’s a pro and a con associated with having (or not having 🫣) accurate address data in your healthcare systems.

Ready to get started?