Intermediate - LogeshVel/go_programs GitHub Wiki

Functions

image

image

image

image

image

image

image

image

image

image

image

image

image

image

Since Slice is already a pointer to an array. SO just passing the value of slice we could change the original values of the slice.

For, Slice and Map we can alter the values just by passing the values. For Slice and map there is no need to pass by reference.

image


Function as variables, arguments...

image

image

image

image

image

image

image

image

image

image

image

Here we have i initial value as 0 that have been used in the return function. So Go keeps that value. so whenever the returned fucntion is called that value is incremented. In the print statement we could see that the used print so no new line character.

Object Orientation

Classes and Objects

Golang is an object orientation language. Maybe it doesn't have the class but it achieves the OOPS in a different way.

image

image

image

Encapsulation

image


Go has no class

image

image

We cannot use the built in types as the receiver types.

image

image

Structs

image

image

image

image

image

image


If the receiver type in the method is not a pointer then the entire value is copied in the method call.

image

Pointer Receivers

image

no dereference is needed

image

no reference is needed

image

image

image

But we could make the method with the receiver type as that struct so that the method is associated with that struct.

image

image

Polymorphsim

image

No inheritance in Golang

image

image

Interfaces

image

image

image

image

Concrete type vs Interface type

image

image

image

image

image

Nil dynamic value

image

Nil dynamic type

image

Empty Interface

image

Type Assertion

image

Type Switch

A type switch compares types rather than values.

While working with the empty interface, the type of empty interface may change at the run time. As we know empty interface can hold value of any type. So, before performing any operation with the empty interface we need to type assert it.

We can use Type switch for type asserting and perform some operations.

image

Error Interface

image

image