Category: 05. Go
-
Error Handling
Go programming provides a pretty simple error handling framework with inbuilt error interface type of the following declaration − Functions normally return error as last return value. Use errors.New to construct a basic error message as following − Use return value and error message. Example Live Demo When the above code is compiled and executed, it produces…
-
Interfaces
Go programming provides another data type called interfaces which represents a set of method signatures. The struct data type implements these interfaces to have method definitions for the method signature of the interfaces. Syntax Example Live Demo When the above code is compiled and executed, it produces the following result −
-
Recursion
Recursion is the process of repeating items in a self-similar way. The same concept applies in programming languages as well. If a program allows to call a function inside the same function, then it is called a recursive function call. Take a look at the following example − The Go programming language supports recursion. That…
-
Range
The range keyword is used in for loop to iterate over items of an array, slice, channel or map. With array and slices, it returns the index of the item as integer. With maps, it returns the key of the next key-value pair. Range either returns one value or two. If only one value is used on the left…
-
Structures
Go arrays allow you to define variables that can hold several data items of the same kind. Structure is another user-defined data type available in Go programming, which allows you to combine data items of different kinds. Structures are used to represent a record. Suppose you want to keep track of the books in a library. You…
-
Pointers
Pointers in Go are easy and fun to learn. Some Go programming tasks are performed more easily with pointers, and other tasks, such as call by reference, cannot be performed without using pointers. So it becomes necessary to learn pointers to become a perfect Go programmer. As you know, every variable is a memory location…
-
Arrays
Go programming language provides a data structure called the array, which can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring…
-
Scope Rules
A scope in any programming is a region of the program where a defined variable can exist and beyond that the variable cannot be accessed. There are three places where variables can be declared in Go programming language − Let us find out what are local and global variables and what are formal parameters. Local Variables Variables that are declared inside…
-
Loops
There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. Programming languages provide various control structures that allow for more complicated execution paths. A loop…