×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

How to name a variable in Golang?

Last Updated : April 19, 2025

In a Go language program, naming a variable is an important part of constructing a program. Since Golang does not care about the variable name but a variable name should be meaningful and descriptive. So, you and others (programmers) may understand the code better.

Go Variable Naming Rules

Go has a few simple rules for naming variables:

  • A variable name must start with a letter (a–z or A–Z).
  • After the first letter, it may contain letters, numbers (0–9), or underscores (_).
  • Variable names are case-sensitive. For example, rollnumber and rollNumber are two different names.
  • Keywords (like func, var, if) cannot be used as variable names.

Example: Choosing a Variable Name

Let’s suppose we want to store the roll number of a student. We might choose a short variable name like r:

r := 101
fmt.Println("Roll number: ", r)

r is a valid variable name, but it is not descriptive. It does not clearly tell us what the value represents.

Using a Slightly Better Name

We can use rn (short for roll number) instead of r:

rn := 101
fmt.Println("Roll number: ", rn)

This is a bit better, but it still doesn’t fully explain the meaning of the variable.

Using a Good Descriptive Name

A much better variable name would be rollNo, rollNumber, or RollNumber:

rollNumber := 101
fmt.Println("Roll number: ", rollNumber)

The name rollNumber clearly explains the purpose of the variable and makes the code easier to understand.

Why rollNumber Is a Good Variable Name

  • The first character is a letter, following Go's naming rules.
  • It includes both "roll" and "number", which tells us exactly what it stores.
  • It uses camel case (first word in lowercase, second word starts with an uppercase letter), which is a common naming style in Go.

Tips for Naming Variables in Golang

  • Use short names for short-lived or temporary variables (like i, j in loops).
  • Use full descriptive names for important values (like userName, totalAmount).
  • Follow consistent naming styles across your codebase.
  • Do not use names that are too generic, like data or value, unless their purpose is very obvious.

Conclusion

Choosing good variable names in Golang helps make your programs easier to read and understand. Always aim for clarity and simplicity. Whether you’re writing a small function or a large application, meaningful names will make your code more professional and easier to work with.

Go Variable Naming Rules Exercise

Select the correct option to complete each statement about Go variable naming rules.

  1. Variable names in Go must start with a ___.
  2. Variable names in Go are ___, meaning Count and count are different.
  3. Exported (public) variables in Go must start with a ___ letter.

Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.