How to name a variable in Golang?

Golang | Naming Variables: In this tutorial, we are going to learn how to name a variable in Go Language?
Submitted by IncludeHelp, on March 25, 2021

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.

A variable must start with a letter and may contain letters, numbers, or underscore (_).

Let suppose, we want to store the roll number of a student, and we used the variable r for it.

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

r is fine as a variable name but it is neither a good nor a descriptive name.

We can use rn as a variable name instead of r.

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

rn is a little bit descriptive but the meaning of the variable still not clear.

A better variable name for it may rollNo or rollNumber or RollNumber.

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

The variable name rollNumber is a very good variable name, because

  • The first character is a letter.
  • It contains the "roll" and "number" both so it is clear that the variable name is using to store the roll number of the student.
  • Here, the first letter of the first word is in lowercase and the first letter of the second word is in uppercase – This variable naming style is known as the Camel case.


Comments and Discussions!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.