Go Naming Rules

Guidelines for naming variables and identifiers in Go

📝 Variable Naming Rules

Go has specific rules for naming variables and identifiers. Following these conventions makes your code readable, maintainable, and helps avoid compilation errors in your programs.


// Valid variable names
var userName string = "alice"
var age2 int = 25
var _temp float64 = 98.6
fmt.Println(userName, age2, _temp)
                                    

Output:

alice 25 98.6

Essential Naming Rules

🔤

Start Character

Must begin with letter or underscore

var name string ✓
var _temp int ✓
🔢

Valid Characters

Letters, digits, and underscores only

var user123 string ✓
var total_sum int ✓
🚫

Case Sensitive

userName and username are different

var Name string
var name string
⚠️

Reserved Words

Cannot use Go keywords

// var if int ✗
var condition bool ✓

🔹 Valid Variable Names

Examples of correct variable naming:

// Valid names
var firstName string = "John"
var lastName string = "Doe"
var age int = 30
var user123 string = "alice"
var _privateVar int = 42
var totalAmount float64 = 1500.75
var isActive bool = true
var MAX_SIZE int = 100

fmt.Printf("User: %s %s, Age: %d\n", firstName, lastName, age)
fmt.Printf("Account: %s, Amount: $%.2f\n", user123, totalAmount)
fmt.Printf("Active: %t, Max Size: %d\n", isActive, MAX_SIZE)

Output:

User: John Doe, Age: 30

Account: alice, Amount: $1500.75

Active: true, Max Size: 100

🔹 Invalid Variable Names

Examples that will cause compilation errors:

// These will NOT work - compilation errors
// var 2name string = "invalid"     // Cannot start with digit
// var user-name string = "invalid" // Hyphen not allowed
// var user name string = "invalid" // Space not allowed
// var if int = 5                   // 'if' is reserved keyword
// var func string = "invalid"      // 'func' is reserved keyword

// Correct alternatives:
var name2 string = "valid"
var userName string = "valid"
var user_name string = "valid"
var condition int = 5
var function string = "valid"

fmt.Println("All valid names work:", name2, userName, user_name)

Output:

All valid names work: valid valid valid

🔹 Go Reserved Keywords

These words cannot be used as variable names:

Go Keywords (25 total):

break case chan const continue default defer else fallthrough for func go goto if import interface map package range return select struct switch type var

🔹 Naming Conventions

Best practices for Go variable naming:

// Recommended naming conventions
var userName string = "alice"        // camelCase for local variables
var UserName string = "Alice"        // PascalCase for exported variables
var MAX_CONNECTIONS int = 100        // UPPER_CASE for constants
var _internalVar string = "private"  // underscore prefix for internal use

// Descriptive names
var customerAge int = 25
var totalPrice float64 = 199.99
var isLoggedIn bool = true

// Short names for short scope
for i := 0; i < 5; i++ {
    fmt.Printf("Index: %d\n", i)
}

fmt.Printf("Customer age: %d, Price: $%.2f, Logged in: %t\n", 
           customerAge, totalPrice, isLoggedIn)

Output:

Index: 0

Index: 1

Index: 2

Index: 3

Index: 4

Customer age: 25, Price: $199.99, Logged in: true

🧠 Test Your Knowledge

Which of these is a valid Go variable name?