Go is the programming language developed by the Google in the year 2007. The Go programming language supports the standard library and distributed packages. Go programming language also manages the external packages. This cheat sheet will give you the information on the basic and advanced topics of the Go programming language such as dynamic typing capability, type safety, garbage collection, variable length array and key-value maps. Whichever service is in high demand, you can get freelance services for the same.
Basic Syntax of Go
The Go programming consists of different tokens. The tokens may be keyword, identifier, string literals, or symbols. Following are the simple “Hello World” program that consists of various tokens.
fmt.Println(“Hello, World!”)
Data Types in Go
The term data type is useful for declaring the variables and functions of different types. The data type also finds the space required for storage and the interpreted bit pattern.
Following are the list of the data types.
Boolean data type: The Boolean data type defines a Boolean value that is true or false.
Numeric data types: The numeric data types define the integer and floating point value.
String data types: The string data types include the set of character.
Derived data types: It includes the array type, structure type, floating point, slice type, and function type.
Variables in Go programming language
The name of the variables can be created from digits, characters, and underscores. The variables start with the letter and underscore. As Go programming language is case sensitive, so the upper and lower case letter of this language is different. Following are the syntax of the variable declaration
var variable_list optional_data_type;
Packages in Go programming language
Packages get used for categorisation of a program, so it is very easy to maintain. The each Go file is associated with some packages. Every Go packages consist of some main packages, so it is easy to compile them. In Go programming, each package is linked together through Import keyword. Following are the syntax for import of packages.
import (
“fmt”
“os”
)
Operators
The Go programming language has rich set operators that work on the operands and perform the particular operations. Following are the types of the operators in Go programming language.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment operators
- Miscellaneous operators
Loops
The loop statements allow us to execute a statements or group of statements multiple times and following are the syntax of the loop statements.
Syntax of For Loop
for [condition | ( init; condition; increment ) | Range] {
statement(s);
}
Syntax of Nested Loop
for [condition | ( init; condition; increment ) | Range] {
for [condition | ( init; condition; increment ) | Range] {
statement(s);
}
statement(s);
}
Function:
The Go programming has various in built function that you are call in the program. For ex Count() is the function which perform the counting operation. Go programming language also allows you declare your own function in program. Following are the syntax of the Go programming language.
func function_name( [parameter list] ) [return_types]
{
the body of the function
}
Array in Go
The array is the collection of similar type of data type stored in contiguous memory location. In an array, lowest address is for the first element and highest address is assigned to the last element.
Syntax for declaration of array
var variable_name [SIZE] variable_type
Pointer in Go
The pointer is the variable which stores the address of the other variables through pointer concept the programming task of the Go programming language becomes easy. Following are the syntax for the pointer variable
var var_name *var-type
Structure in Go
Go programming language supports the concept of the structure which allows you to combine the data items of different kinds. Following are the syntax of the structure definition.
type struct_variable_type struct {
member definition;
member definition;
…
member definition;
}
Maps in Go
Go provides the special type of data type that is a map. The map keyword maps unique keys to values. Following are the syntax for defining the map.
* declare a variable, by default map will be nil*/
var map_variable map[key_data_type]value_data_type
/* define the map as nil map cannot be assigned any value*/
map_variable = make(map[key_data_type]value_data_type)
Interface in Go
The other important data type in Go programming language is an interface. The interface represents a set of a method signature. Whichever service is in high demand, you can get freelance services for the same. Following are the syntax for the interface in Go programming language.
* define an interface */
type interface_name interface {
method_namea [return_type]
method_nameb [return_type]
method_namec [return_type]
…
method_namen [return_type]
}
/* define a struct */
type struct_name struct {
}
/* implement interface methods*/
func (struct_name_variable struct_name) method_namea() [return_type] {
Statements;
}
func (struct_name_variable struct_name) method_namen() [return_type] {
Statements;
}
Summary:
This article is on the Go programming cheat sheet. This cheat sheet is helpful for those programmers who work on the Go programming language. This cheat sheet provides the programmer basic and advanced syntax of Go programming language.