The way you declare a variable or function, and the naming convention you use are very important, and some case differences affect scope too. In this lesson, we will learn :
- What names to use?
- What convention is recommended?
- Scope of variable and function names
1. What names to use?
In Go, variable, functions, constants, packages..etc have names that start with either a letter or underscore. That is, the following names are allowed :
var myName string = "Mohamed" //variable name with camel case
var _age int = 30 //variable name starting with an underscore
myFunction //function name with camel case
_my_function //function name with underscores
myFunction30 //function name that ends with digits
const PI = 3.14 //constant names can start with a capital
But the following name will cause an error:
var 1myName //starts with a digit
var my-Name // "-" is not allowed
const #Pi // '#' is not allowed
my Function // spaces are not allowed
That, and 24 keywords that are not allowed because they are already reserved, You cannot declare variables with the following names :
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
And there are other names that can be used, but are not recommended as they can be a source of confusion (although there are cases where it is useful to re-declare them) :
true false iota nil
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
float32 float64 complex128 complex64
bool byte rune string error
make len cap new append copy
close delete complex real imag panic
recover
2. What convention is recommended ?
As for the convention used for names, I personally prefer the Camel Case , take some time to read about it. Following are some names that respect the Camel Case naming convention :
myName
carFuelTank
housePrice
guestFullName
dbPassword
Camel-cased names often start with a small letter. If the name is a compound of many words, then every other word (except the first) starts with a capital, asYouCanSeeAllWordsAreInCapitalsExceptTheFirstAs !
In Camel Case convention, underscores are not used to separate words, so the following names do not respect this convention (I hate names with underscores and digits) :
my_name
car_fuel_tank
db_password
get_house_price
Of course, there is an exception to this : exported variables, We will see what these are soon.
3. Scope of variable and function names
When you declare a variable inside a function, that variable is only visible to that function. That is: You can only call the variable and use it inside the function's body.
To make the variable myVariable visible to all files and functions that reside in a given package pkg, i.e : all the files that start with package pkg , you have to declare the variable outside functions, just after the import instructions, like this :
package pkg
import "fmt"
var myVariable float32
// ... functions go here...
So, in the previous example, any file that starts with package pkg can use myVariable directly.
What if we want to use myVariable in a file that does not belong to the package pkg?
In this case you have to export the variable myVariable, export here means that the variable will become visible to all the packages that import our pkg package. And to export a variable or function...etc, All you need to do is capitalize it, so we change myVariable to MyVariable, as follows :
package pkg
import "fmt"
var MyVariable float32
// ... functions go here...
Then, to use the variable MyVariable from a file that belongs to a different package (say package pkgoo ) , we have to import pkg , and use pkg.MyVariable to use the variable (same thing for functions...etc).
package pkgoo
import "pkg"
// call a function here ..
myVar := pkg.MyVariable
As you can see in the previous example, the file in the pkgoo package imports the pkg package , then uses the exported variable with pkg.MyVariable, We will see full examples in future lessons about packages and files.
That ends our lesson, in the next lesson we will learn about the basic types (like int, float32, string, boolean...etc).