Naming Conventions
Part of the Fundamentals section of Coddy's GO journey — lesson 13 of 109.
Let's learn about naming conventions in Go, which are important patterns that make our code easier to read.
First, let's see how to name variables using camelCase (starting with lowercase and capitalizing subsequent words):
var userName = "Alex"
const maxLoginAttempts = 3
fmt.Println(userName)
fmt.Println(maxLoginAttempts)After executing this code, we'll see our variable values displayed on the screen:
Alex
3In Go, we follow these simple naming rules:
- Use camelCase for variables and functions (like
userName) - Avoid using snake_case (like
user_name) - Don't use PascalCase (like
UserName) for local variables - Important: Names that start with an uppercase letter (like
UserName) are exported and visible outside packages
Following these naming conventions makes your Go code more readable and consistent with standard Go practices.
Challenge
BeginnerIn this challenge, you'll practice using proper Go naming conventions. You need to fix the variable names in the code to follow Go's standard naming conventions:
- Use
camelCasefor variable names (start with lowercase, capitalize subsequent words) - Use
PascalCasefor exported variables (start with uppercase) - Avoid using underscores in variable names
Fix the variable names in the code to follow these conventions, then print them.
Cheat sheet
Go uses specific naming conventions to make code readable and consistent:
Variable and function names: Use camelCase (start with lowercase, capitalize subsequent words):
var userName = "Alex"
const maxLoginAttempts = 3Exported names: Use PascalCase (start with uppercase) - these are visible outside packages:
var UserName = "Alex" // exportedAvoid:
- snake_case (like
user_name) - PascalCase for local variables
- Underscores in variable names
Try it yourself
package main
import "fmt"
func main() {
// TODO: Fix these variable names to follow Go naming conventions
var user_name string = "John"
var USER_AGE int = 25
var is_active bool = true
var FIRST_login string = "2023-01-15"
// TODO: Fix these variable names to follow Go naming conventions
fmt.Println("User:", user_name)
fmt.Println("Age:", USER_AGE)
fmt.Println("Active:", is_active)
fmt.Println("First Login:", FIRST_login)
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions