The name of go functions, variables, constants, types and packages abide by following rules:
- Name begins with a letter or underscore
- Name can have any number of additional letters, digits as well as underscores
- Case in name matters; list and List are two different names
- Go has 25 reserved words like if, map, package, go etcetera
- If name begins with uppercase, it can be used outside it’s own package; example Printf of fmt package
- Package names are in lower case
- Names go bigger and have more explainable name as their scope increases, for example, name i is perfectly fine for a small loop
- Name follows camel case notation
- Go program is stored in one or more files that ends with .go extension
- Each file begin with a package name that says which package this file is part of
- Package declaration is followed by the import declaration
- import declaration is followed by package level declaration of constants, types, variables and functions in any order
Here, boilingF is package level entity, i.e. visible through out the package.
Output of the code is:
boiling point in Centigrade 100 and in Fahrenheit 212
var name type = expression var name = expression var name type
All three declarations are legal. If the type is omitted as in the second initialisation, type is inferred from the expression. If the expression is not given as in the third case, variable is initialised with zero value which is “” for strings, 0 for numbers and false for booleans and nil for others.
- There is no uninitialised variable in Go
- var i, j, k int – Initialises three variable of type integer
- var f, err = os.Open(name) – Method Open returns and initialises file and error
Short Variable Declaration
t := 0.0
In the expression above, we have declared and initialised a variable called t with 0.0.
- Takes the form of variableName := initialisation
- Used for the majority of the local variable
- If the variable is already declared in that lexical block, short variable declaration acts as assignment
A variable stores some value. A pointer has address of the variable.
Consider the example above.
We have initialised a variable called f whose value is 100. Now, we have a variable called p which stores the address of f. &f basically gives the address of the f. We can access the value stored in that address by *p.
The output of the program above is:
value of f is 100 and the address where f is stored c000016040 value of f is 200 and the address where f is stored c000016040
The new Function
p := new(int)
New Operator is another way of declaring the variable.
There are variables that share the same representation but they vary in the significance. And int may be loop index or timestamp or goal scored.
type Score int
In this example, we have made a type called Score where the underlying type is int.
- Type Declaration occurs at package level
- Conversion from one type to the other is allowed if the underlying type is same
- Two values of different named type can not be compared directly
Package And Files
Packages in Go are for the same reason as libraries or modules in other languages. They provide a way for encapsulation, modularity as well as reuse.
- In Go, exported identifiers start with an upper-case letter. Export is same as public method/variable/enum in Java or c++
- It is an error to import a package and then not use it
- It is suggested to use goImports tool which takes care of insertion and removal of imports
- Package level variables are initialised in the order they are declared except for the dependencies which takes precedence
- In case of multiple .go files, compiler is given .go file in the sorted order by name
- There are init methods that can not be called or referenced
- init functions are automatically executed when the program starts in the order in which they are declared in the file
- one package is initialised at a time in order of the imports in the program. So, a package p that imports q can be initialised knowing fully well that q is initialised
- main package is last to be initialised
The Scope of a declaration is the part of the code where a use of declared name refers to that declaration.
- Scope of declaration is the region of code
- Scope is compile time property
- Lifetime of a variable is duration of time when it is referenced by other part of the program
- Lifetime is run time property