Golang

Golang is an open-source programming language created and supported by Google. It's a very easy language. Golang is one of the most desired languages to learn and it's also one of the most popular programming languages. Golang was created by the engineers at Google.

.Golang was created to combine-

.The ease of the programming of an interpreted, dynamically typed language(such as python)

.With the efficiency and safety of a statically typed, compiled language.

. It also aimed to be modern with support for network and multicore computing.

Your First Go Program

package main

import "fmt"

func main(){

fmt.println("good night")

}

  • package main:-Every Go program must start with the package declaration

  • import "fmt":-We use import keywords to include code from other packages to use with our program. Import fmt basically means that we are importing the fmt package in our code.

  • func main():-The main function is a special type of function and it is the entry point of the executable programs.

  • **fmt.println("good night"):-**First, we specify the name of the package to be used, which is fmt.Then we specify the name of the function that's present inside that package. Here the function's name is println, which means print, The third part is the text HelloWorld enclosed within the quotes, anything within the quotes becomes a string.

What is a Data Type

A data type is a classification made on the kind of data. They categorize a set of related values and describe the operations that can be done on them.

For example

. we have string, which is a sequence of characters and is used to represent text.

.Then we have numbers, which consist of integers and floats.

.Integers in Golang are like integers in mathematics.

.Float are numbers that contain a decimal counterpart such as 7.5,2.66.

Boolean consists of just two values:-

true and false.

.Array and slices:- An array is like a list of elements of a single data type.

slices represent a flexible array-like data type.

. Maps:- A map is a collection of key-value pairs, For example, we can have a map of string to integer ("x" -> 10)The key x over here refers to the integer value 10.

Variables

The variable is a name given to a storage area that the programs can manipulate.

There are two kinds of variables, local and global variables.

Local variables are declared inside a function or a block and are termed local variables. These are not accessible outside the functional block and these variables can also be declared inside looping or conditional statements.

for example:-

package main

import "fmt"

func main(){

name:=yash

fmt.println("name")

}

Global variables are declared outside of the functional block. They are available throughout the lifetime of a program They're declared at the top of the program outside all functional blocks.

for example:-

package main

import "fmt"

var name string = "yash"

func main(){

fmt.println("name")

}

User Input

This is an example of user input:-

package main

import "fmt"

var name string

func main(){

fmt.print("Enter your name")

fmt.scanf("%s",&name)

fmt.println(name)

}

Operators and Control Flow

operators are symbols that help us to perform specific mathematical and logical computations on operands.

for example:-

a+b:- Here (+) addition sign is an operator while a and b are the operands.

There are 5 types of operators

  1. comparison operators

    Equal to(==),not equal(!=),less than(<),greater than(>),less than or equal to(<=),greater than or equal to(>=).

  2. Arithmetic operator

    Addition(+),subtraction(-), multiplication(*), division(/),modulus(%), increment(++) and decrement(--).

  3. Logical operator

    And operator(&&),OR(||),Not(!).

  4. Assignment operator

    assign(=),add and assign(+=),subtract and assign(-=),multiply and assign(*=).

  5. Bitwise operator

    bitwise And(&),bitwise OR(|),bitwise XOR(^)

Arrays

An array is a collection of similar data elements that are stored at contiguous memory locations.

Why do we need an array?

we need to store a large amount of data of a similar type.

Array declaration syntax:-

var <array name> [size of array] <data type>

We have a var keyword, the name of the array, the size of the array and the data type.

Slices

slices provide access to parts of an array in sequential order. They are more flexible and more powerful than arrays since arrays had limitations of being fixed size. Unlike arrays, they are variable types. That is, you can add and remove elements from a slice. slices have three major components Pointer, length, and capacity. The pointer is used to point to the first element of the array that is accessible through that slice.It is not necessary that the pointed element is the first element of the array.

The declaration is similar to creating an array:-

< slice_name> := [ ]<data_type>{<values>}

example= marks := [ ]int {10,20,30}

Here is an example of how to declare a slice:-

package main

import "fmt"

func main(){

marks := [ ]int {10,20,30}

fmt.println(marks)

}

When we say that we want to slice this array from index zero to index three, this means we want to include elements from index zero to index two in our slice.

array[start_index : end_index]

array[0 :3]

Note, we do not include the end index element in our slice.

array[ :4 ] =it means the array starts from 0 to 4.

array[ : ] =it means the array starts from 0 to end.

Now, there's one more way of declaring a slice and that is through the make function. The make takes three parameters, the data type, the length, and the capacity. Capacity is optional.

slice := make ([] int,5,10)

Let's create a slice using the make function.

Appending to a slice

Now we can append a slice to another slice by simply using the three dots.

syntax:- slice=append(slice 1, anotherslice...)

example:-

copying from a slice

syntax:-

fun copy(dst, src []Type)int

Maps

syntax

var<map_name>map[<key_datatype>]<value_datatype>

var my_map map[string]int

To create maps with key-value pairs, we need to initialize them as well.

<map_name> :=map[ <key_datatype>]<value_datatype>{key-value-pairs>}

getting a key

Functions

A function is a group of statements that together perform a task. Every Go program has at least one function, which is main(). You can divide your code into separate functions.

Function syntax:=

func <function_name>(<params>) <return type>{

// body of the function

}

return a keyword

func addnumber(a int,b int){

sum :=a+b

return sum

}

How to call a function

syntax= <function_name>(<argument(s)>)

Recursive Functions

Recursive is a function that calls itself by direct or indirect means. The function keeps calling itself until it reaches a base case.

High Order Functions

High order function is a function that receives a function as an argument or returns a function as output.

Why do we use high order function

. Composition

We can create smaller functions that take care of a certain piece of logic, and then we can compose more complex functions by using these different smaller functions.

example:-

Pointers

A pointer is a variable that holds the memory address of another variable.

syntax of declaring and initializing:-

var <pointer_name>*<data_type>

Initalizing a pointer=

var <pointer_name>*<data_type>=&<variable name>

i:=10

var ptr_i *int =&i