🔑 Go Identifiers & Keywords – Rules & Reserved Words in Go (2025 Guide)
🧲 Introduction – Understand Go’s Building Blocks
Every programming language has keywords (reserved words) and identifiers (names we define). In Go, knowing the difference between them is key to writing valid, readable, and maintainable code.
🎯 In this section, you’ll learn:
- What identifiers and keywords are in Go
- Rules for naming identifiers
- List of all reserved keywords in Go
- Best practices and examples
🆔 What Are Identifiers in Go?
An identifier is the name you assign to variables, constants, functions, types, or packages. It acts as a label for referring to data or logic in your code.
✅ Examples of identifiers:
var username string
const Pi = 3.14
func calculateTax() {}
🚫 Invalid identifier examples:
var 1stUser string // ❌ Cannot start with a digit
var if string // ❌ Cannot use a reserved keyword
📋 Rules for Naming Identifiers
Rule | Description |
---|---|
Must begin with a letter (A–Z or a–z) or underscore _ | |
May contain letters, digits, or underscores | |
Case-sensitive – Total and total are different | |
Cannot match a reserved keyword |
✅ Valid Examples:
count, _value, HTTPResponse, user1
❌ Invalid Examples:
1value, var, func, 99Balloons
🧠 Note: Identifiers starting with a capital letter are exported (public) when used in packages.
📕 List of Reserved Keywords in Go
Go has 25 reserved keywords. These words are built into the language and cannot be used as identifiers.
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
They are used to define the syntax and structure of the language.
🧪 Example – Identifiers and Keywords in Use
package main
import "fmt"
const appName = "GoApp"
func main() {
var user string = "Alice"
fmt.Println("Welcome to", appName)
fmt.Println("User:", user)
}
📤 Output:
Welcome to GoApp
User: Alice
appName
,user
, andmain
are identifiers.const
,var
, andfunc
are keywords.
🔒 Identifier Visibility: Exported vs Unexported
Identifier | Starts With | Visibility |
---|---|---|
TitleCase | Capital letter | Exported (Public) |
camelCase | Lowercase | Unexported (Private) |
Example:
// Exported function
func Add(x, y int) int {
return x + y
}
// Unexported function
func subtract(x, y int) int {
return x - y
}
This rule is important when using or writing Go packages and modules.
🧠 Best Practices for Naming Identifiers
✅ Use meaningful names (userCount
, responseTime
)
✅ Use camelCase for variables and functions
✅ Use TitleCase for exported identifiers
✅ Avoid short names like x
, y
, z
— unless for loops
✅ Don’t shadow standard library names (e.g., avoid naming a variable string
or error
)
📌 Summary – Recap & Next Steps
Identifiers and keywords form the language core. Identifiers give meaning to your logic, while keywords define how that logic works. Stick to naming rules, avoid reserved words, and use consistent naming conventions for readable code.
🔍 Key Takeaways:
- Identifiers name your variables, constants, and functions
- Keywords are reserved and cannot be used as names
- Use capitalized identifiers to export symbols from packages
- Follow Go naming conventions for clarity and compatibility
⚙️ Next: Dive into Go Data Types – understand how Go handles primitive, composite, and custom types.
❓ FAQs – Identifiers and Keywords in Go
❓ Can I name a variable int
or func
?
✅ No. These are reserved keywords and cannot be used as identifiers.
❓ Are Go identifiers case-sensitive?
✅ Yes. Data
and data
are treated as two different names.
❓ What’s the difference between keywords and identifiers?
✅ Keywords define language structure (if
, for
, func
), while identifiers are names defined by the programmer (username
, AppName
).
❓ How do I export a function from a package?
✅ Start the function name with an uppercase letter. Example: PrintMessage()
is exported, but printMessage()
is not.
❓ Why use camelCase and TitleCase in Go?
✅ camelCase is the Go convention for local names. TitleCase is for exported (public) names, used by tools like godoc
.
Share Now :