Skip to main content

Posts

Showing posts from March, 2021

Controlling Program Flow of golang

  Overview In this blog I am going to talk about the topic below. Loop break continue infinity collection Panic Switches Loop You can use loop as below. here i variable is declared inside the loop so the value will be valid in side the loop.  Example       package main import ( "fmt" ) func main() { for i:=0;i<5;i++{ fmt.Println(i) } } output : 0 1 2 3 4 5 Break in Loop what Break statement dose is it will break out from the loop.          package main      import (      "fmt"      )      func main() {      for i:=0;i<5;i++{      fmt.Println(i)      if i==2 {      break      }      }      } output : 0 1 2 in the example you can see when loop comes to the point when i == 2 then it break out from the loop and end the program. this why it out put the value of i until it is

Functions and Methods of golang

  Overview In this blog i am going to talk about the functions and methods in golang. which is a important topic in go. if you have not read the previous blog or do not have a basic knowledge about go the i would suggest you to have a look at my other blog.  Functions   Methods Functions you can declare a function as below. you don't need to declare return type unless your not returning something from your function. you get the return value in a way that is explained in the example code . func <function name > ( <parameter> <parameter type> ) ( <return type> ){} Example Code : package main import ("fmt") func main() { test1() // example 1 sum := test2() // example 2 fmt.Println("sum =",sum) sum2,error:= test3(30,40) // example 3 fmt.Println("sum 2 =",sum2,error) sum3,_:= test3(80,60) // example 4 fmt.Println("sum 2 =",sum3) su

Collections of Golang

  Overview I have discussed data type and other basic stuff on the previous blog. Pleas check that blog by clicking on this link  . in this blog i am going to talk about the topic below Array Slice Map Structs   Array An array is a fixed size collection of similar data type .  In golang you can declare array in two way implicate and explicate. in explicate initialization you need to initialize array as bellow.      var < name> [< size> ] < type> in implicate initialization you need to initialize array as bellow.      <name> := [ <size> ] <type> {} for implicate initialization you don't have to say size of the array if your not sure of the size. compiler will automatically increase the size as you go on farther. Code : var i int package main import ( "fmt" ) func main() { var arr [3]int arrSecond := [3]int{15,45,85} arrthird := []int{15,45,85} arr[0] = 15

Primitive data type of Golang

Overview In this blog I am going to talk about the date types and variables in go. and the topics bellow  Declaring and initializing variables   explicit initialization of variables   implicit initialization of variables complex data type list of data type Pointer pointer operator dereferencing operator address of operator Constant Iota and constant expression Declaring and initializing variables there are 2 ways you can declarer and initialize a variable they are explicit initialization variables  implicit initialization variables  Explicit initialization of variables  in this way you have to tell the compiler every thing you need to declare a variable. var i int i = 20 var j int = 3 fmt.Println(i,j) var f float32 = 32.233 fmt.Println(f)  in this way we are explicitly telling what will be the input type for that variable. Implicit initialization of variables  in this way of declaring a variable. we don't have to tell the compiler what type of data it will take on that

List of data types in Golang

  index Description 1 Type: int8 8-bit signed integer 2 Type:  int16 16-bit signed integer 3 Type:  int32 32-bit signed integer 4 Type:  int64 64-bit signed integer 5 Type:  uint8 8-bit unsigned integer 6 Type:  uint16 16-bit unsigned integer 7 Type:  uint32 32-bit unsigned integer 8 Type:  uint64 64-bit unsigned integer 9 Type:  int Both in and uint contain same size, either 32 or 64 bit. 10   Type:  uint Both in and uint contain same size, either 32 or 64 bit. 11 Type:  rune It is a synonym of int32 and also represent Unicode code points. 12 Type:  byte It is a synonym of int8 . 13 Type:  uintptr It is an unsigned integer type. Its width is not defi

Pointer in Golang

  Over view In this blog I am going to talk about pointer and how it works. will talk about  How variable stored in memory ? Pointer Dereferencing How variable stored in memory? to understand pointer one need to know how computer store its  value in memory. By memory i will be refereeing to the RAM. basically computer stores its variable in memory in binary format. and when some one declares a variable it will allocate some bite of memory for that variable. And when variable is initialized it will go to its look up table and search for the first memory address and store data. for int and float it will allocate 4 and char it will allocate 1 byte of memory   suppose I have declared  'a'  as variable which is type int. computer will allocate 4 byte of memory for that and if i declared 'c' as variable with a type char. it will allocate a 1 byte of memory for this. and it will put the first memory address to its lookup table to search for the variable later. then if I ini

Creating a project with Golang

Overview    You can run a go file with out making a project. But the standard way to use go is to create a project or you can call it a space where you will be putting your go code, on the other hand it is a work space, but in Go Lang we call it module. In Go Lang modules are the official way of organize go code. in this blog I am going to explain the way you create a model and first hello world with that project. Explanation To Module you need to run the command bellow go mod init <module name>  If you can see I have named my module github.com/TahimFaisal/golang. the name of the module is called module initializer. this is the standard way of initialization. And for the go get command it will it will know where to go and get the mod if it cannot find the mod locally.  Okay, soon after you ran the command you will get a file named go.mod in your working directory  it will be having module initializer and the version of go your using.     now create a main.go file and write