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!

Load comments ↻





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