Basics
Go basics
In this section some basics of go are noted down.
Basic data types
Go's basic data types are,
Ref: A Tour of Go
Variables
Variable is the name given to a memory location to store a value of a specific type.
Declaring single variable
Syntax: var name type
Example:
Declaring variable with initial value
Syntax: var name type = initialvalue
Example:
Type inference
If a variable has an initial value, Go will automatically be able to infer the type of that variable using that initial value.
Syntax: var name = initialvalue
Example:
Multiple variable declaration
Syntax: var name1, name2 type
Example:
With initial value
Variables of different types
with initial value
Short hand declaration
Syntax: name := initialvalue
Example:
Short hand syntax can only be used when at least one of the variables in the left side of := is newly declared.
Ref: GOLANGBOT.COM
Constant
Once declared, can not be reassigned,
Syntax: const name = value
Example:
The value of a constant should be known at compile time.
b is a constant and the value of b needs to be know at compile time. The function math.Sqrt(4) will be evaluated only during run time and hence const b = math.Sqrt(4) throws error.
Go is strongly typed language. So, mixing type during assignment is not allowed.
Hence,
Although myString
is alias of string
, Go does not allow to assign string
type variable into myString
type variable.
Constant does not have a type. They can provide a type on the fly depending on the context.
For example,
Output:
Again,
Output:
This program does not give any error because a is constant, so it does not have any type. When a is assigned to different type variable, it is taking type of this variable.
Ref: GOLANGBOT.COM
Functions
A function is a block of code that performs a specific task.
Structure:
Sample function:
If consecutive parameters has same type, writing type each time can be avoided by writing once at the end.
Multiple return value:
Named returned value:
If a return value is named, it will be considered as a variable declared at the first line of the function. They don't need to return explicitly.
Blank identifier: _ is know as the blank identifier in Go. It can be used in place of any value of any type. It is used to discard a returned value.
Packages
Packages are used to organise go source code for better reusability and readability.
main function and main package
Every executable go application must contain a main function. This function is the entry point for execution. The main function should reside in the main package.
First line of every go source file should be
package packagename
which indicate that this source file belong to which pakage.
Source files belonging to a package should be placed in separate folders of their own. It is a convention in Go to name this folder with the same name of the package.
Package Example:
Sample Go project structure:
rectangle package:
main package:
Exported Names
Any variable or function which starts with a capital letter are exported names in go. Only exported functions and variables can be accessed from other packages.
init function
Every package can have init function.
The init function should not have any return type and should not have any parameters.
The init function cannot be called explicitly in our source code.
init function structure:
The init function can be used to perform initialisation tasks and can also be used to verify the correctness of the program before the execution starts.
The order of initialisation of a package:
If a package imports other packages, the imported packages are initialised first.
Package level variables are initialised then.
init function is called next. A package can have multiple init functions (either in a single file or distributed across multiple files) and they are called in the order in which they are presented to the compiler.
A package will be initialised only once even if it is imported from multiple packages.
Initialisation Example:
rectangle package:
main package:
Output:
The order of initialisation of main package,
Imported package are initialized first. Hence, rectangle package will be initialized first.
Package level variable
rectLen
andrectWidth
are initialized then.Then, init function of main package is called.
Finally, main function is called.
Use of blank identifier
Go does not allow to import any package without using it. So,
If we want to import a package without using now but we may use later.
Or, we want to make sure that initialization of a particular pakage happen even though we don't want to use it.
We can use blank identifier ( _ ).
Conditional statement (if else)
Simple if
structure:
if-else
statement:
if - else if - else
chain:
if
variant:
Loop
Go has only for loop.
for
loop structure:
Example:
break
continue
Variant:
Infinite loop:
Switch Statement
Structure:
Multiple Expression:
Expressionless switch:
switch is considered to be switch true
and it will behave like if - else if
chain.
Fallthrough:
When a case is executed, program control get out of the switch. A fallthrough
statement is used to transfer control to the first statement of the case that is present immediately after the case which has been executed.
Switch and case expressions need not be only constants. They can be evaluated at runtime too. In the program above num is initialised to the return value of the function number().
If the number is 75 then output will be.
fallthrough
should be the last statement in a case. If it present somewhere in the middle, the compiler will throw error fallthrough statement out of place
Last updated