Skip to main content

Collections of Golang

 

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
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
        arr[1] = 12
        arr[2] = 13

        fmt.Println(arr,arrSecond,arrthird)	
    }
    

Out Put :

    [15 12 13] [15 45 85] [15 45 85]

Slice

Slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. slice is built on top of an array. A slice does not store any data, it just describes a section of an underlying array. Changing the elements of a slice modifies the corresponding elements of its underlying array. Other slices that share the same underlying array will see those changes.

On other words slice divide an array and make a new array and it works link a pointer. If you change on slice the change will reflect on the array it was made from.

if you want to make a slice on array named 'arr' then a you need to do as below.

     <slice name> := <array name>[<starting point>:<ending point>] 

Code :

    package main
    import ("fmt")

    func main() {
    
        arr := []int{32,12,45,62,43}

        slice1 := arr[:]
        slice2 := arr[1:]
        slice3 := arr[:2]
        slice4 := arr[1:2]

        fmt.Println(slice1,slice2,slice3,slice4)

        slice1[0] = 3
        fmt.Println(arr)
        

    }

Out Put :

        [32 12 45 62 43] [12 45 62 43] [32 12] [12]
        [3 12 45 62 43]

Note:  as you can see I have changed on 'slice[0]'  but printed 'arr' and can see the change what i have made in  'slice[0]'  in 'arr' .

Map

Map is a collection where we can associate key to the value.you can initialise map as below
    <name> := map[<key type>]<value type>{<value>}

Code :

  
    package main
    import ("fmt")

    func main() {
        m := map[string]int{"foo":21}
        fmt.Println(m,m["foo"])

        delete(m,"foo")
        fmt.Println(m)
    }
  

Out Put :

        map[foo:21] 21
        map[]

Struct

struct is the only collection type that allows us to associate disparate data type together. In array, slice and map we cannot associate different type of data but in struct we can. The only limitation that we have with struct that is the field that we make available are fixed in compile time. 

you cannot define and initialize struct in one line. first you need initialize struct and make an object  to initialize the struct.   
	type < name of struct > struct {
            < key name > < value type >
        }
        
        var < variable name > < name of struct >
		
        < variable name >.< key name > = < value >

Code : 

      
      type user struct {
	ID int
	firstName string
	lastName string
      }

      var u user

      u.ID = 1
      u.firstName = "hello"
      u.lastName = "world !!!"

      fmt.Println(u)

      fmt.Println(u.ID)
      fmt.Println(u.firstName)
      fmt.Println(u.lastName)

Out Put :

        {1 hello world !!!}
        1
        hello
        world !!!

Previous Blog Next Blog
Primitive data type of Golang Functions of Golang

















Primitive data type of Golang

Comments

Popular posts from this blog

CRUD with Golang

  Overview this blog will be about a  basic crud app with golang .   this app will provide API to store, update, delete information . you need to know the basics of golang to understand this project. if you do not have the basics knowledge  about golang I would suggest you to read my previous blog.  I have uploaded my code in  GitHub. click the link to clone or download this project  I am going to talk about the topic below. Project setup Data handling controller Project setup  Initiate mod and set up project as per golang. if you don't know how to setup golang project i would suggest you to have a look at my blog  Creating a project with golang . this is a really short blog. you will have a quick over view of how to setup a project with golang.  so now that you have setup the project then the folder structure will be as below. Data Handling data store, delete and update will be handled in the user module. code is given below  package moduls import ( "errors&quo

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

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