Golang : Pass by Value Vs Pass by Refere

By default every thing in go is pass by value, which means if you pass any argument to a function, it would copy the values passed in another variable and make changes to it in function scope only (with in function block) and does not modify the original value.

Which means whatever you pass to a function in Golang remains immutable and does not get modified. However, if you use pointer then changes would impact the original value.

Try to read below program and understand it:

package main

import "fmt"

type Person struct {
       firstName string
       lastName  string
}

func changeName(p Person) {
       p.firstName = "Bad"
       fmt.Print("inside method with local scope on copy")
       fmt.Println(p)
}

func changeNameWithPointer(p *Person) {
       p.firstName = "Bad"
       fmt.Print("inside method with local scope on pointer")
       fmt.Println(p)
}

func main() {
       person := Person{
              firstName: "Good",
              lastName:  "person",
       }
       fmt.Println("Original value of person", person)
       changeName(person)
       fmt.Print("Outside method with original variable passed as copy")
       fmt.Println(person)

       fmt.Println("\n\n========Pass by pointer will modify values =======")

       fmt.Println("Outside method with original variable", person)
       changeNameWithPointer(&person)
       fmt.Println("Outside method with original variable passed as reference (which is now changed )")
       fmt.Println(person)
}

 

Sample output :

➜ passByValue git:(master) ✗ go run ValueVsReference.go
Original value of person {Good person}
inside method with local scope on copy{Bad person}
Outside method with original variable passed as copy{Good person}
========Pass by pointer will modify values =======
Outside method with original variable {Good person}
inside method with local scope on pointer&{Bad person}
Outside method with original variable passed as reference (which is now changed )
{Bad person}

 

 

 

AngularJS 2.0 : Environment setup

Here are simple steps you need to follow to get Angular Js 2.0 application up and running on your box.

# Make sure you have latest nodejs download link>   https://nodejs.org/en/

# Using npm install type script from command line>  npm install g typescript

# Download latest angular cli too > npm install g angularcli@latest

You are all set up create your new dream app, Angular CLI tool would create the basic structure required for a new app, since you have environment ready, lets create your dream app and run it on cli server

# Run Below commands, it would create you app.

$ng new my-dream-app

$cd my-dream-app

$ng serve

 

 

You might want to download atom IDE with supporting package from  https://atom.io/

Java Tutorial : Hands on project for library management application

I am coding Library Management Project in Java and teaching it in simple steps.
If you are interested in doing or learning Java with hands on project then check out this play list from my youtube video 🙂

Git Repo :
https://github.com/ProjectsbyPrem/LibraryManagement

Project requirements:
https://docs.google.com/document/d/1ZzAuMQ5jqCwh1qvF5DIvuVP6J1QwUugTuQ1uQjIXRkg/edit
For details visit : http://www.premaseem.com

NodeJs : Convert json to csv using node module

Its very common usecase to convert json docs or Mongo doucments into csv and share with other. I am sharing a simple node module with example to accomplish the same.

Details of package - https://www.npmjs.com/package/json2csv

# To install node module from cmd 
npm install json2csv --save



# create a converter.js javascript file as below with you input json and mapping.

var json2csv = require('json2csv');
var fs = require('fs');
var fields = ['car.make', 'car.model', 'price', 'color'];
var myCars = [

    {
        "car": {
            "make": "Hundai", "model": "sonata"
        },
        "price": 30000,
        "color": "white"
    },
    {
        "car": {
            "make": "Audi", "model": "A3"
        },
        "price": 40000,
        "color": "blue"
    }, {
        "car": {"make": "BMW", "model": "F20"},
        "price": 35000,
        "color": "black"
    }, {
        "car": {"make": "Porsche", "model": "9PA AF1"},
        "price": 60000,
        "color": "green"
    }
];
var csv = json2csv({ data: myCars, fields: fields });

fs.writeFile('file.csv', csv, function(err) {
if (err) throw err;
console.log('file saved');
});


# now run javascript file on node
node converter.js

# This will create a csv file in the current directory called as file.csv open it in excell and you are done.