How do you ensure code quality in Go? This post is a definitive guide on the best practices for writing, structuring, and organizing code when developing in Go.
Code quality always matters. It’s like having a good handwriting only, its for code. Your piece of code should be easy to locate, readable and well documented. Variables, functions, file names should be pretty self explanatory and long source code files should be generally. No one wants to rummage through lines of cryptic code trying to decode what it’s doing there.
Remember, you spend only 20% of your time coding. The other 80% goes in reading code written by someone else. So write your code in such a way so that someone else can easily read, understand, refactor and maintain it in the future
Go by design is simple to read, so things become a bit easier. However here are certain things to keep in mind when writing applications for production which will make you look like a pro Go developer.
Naming Conventions
Variables:
- Use Camel case for naming your variables
var myVariable = "" // DO
var my_variable = "" // DON'T
Note: If a name (a variable or a function) is in title case (eg ThisIsTitleCase) then its said to be an exported variable or function. This means that it can be accessed from outside the package as well.
- Use short but descriptive names for variables
Try to use short names for variables. Name the variable according to its scope, the farther away it is used from its declaration, the longer its name.
var req Request
- Use single letters for indexes
for i:=0;i
