golang tutorial

  1. go object pass by value, so it won’t affect the original object

    1
    2
    3
    4
    5
    6
    7
    8
      func UpdateEmployee(empDetails Employee){
    empDetails.Name = "Anshul";
    }

    var newEmp = Employee{ Name: : "Mayank", Age: 40, Destination: "Developer"}

    UpdateEmployee(newEmp);
    fmt.println(newEmp)
    1. if you want pass go object by reference, you need to use reference syntax, * and &
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
       func UpdateEmployee(empDetail *Employee){
      empDetail.Name = "Anshul";
      }
      var newEmployee = Employee{Name: "fdfdsf", Age: 40, Destination: "fdfdsfdsf"};

      UpdateEmployee(&newEmployee);
      //& is used to extract the reference of a object
      // * is used to attacked the reference of an object.

      //the original object sent to the function updated

      fmt.Println(newEmployee.Name);

    if using the new keyword to create a new object, it will returns the address of the object. that is reference.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     type Employee struct {
    Name string
    Age int
    Designation string
    Salary int
    }

    var newEmployee = new(Employee)

    fmt.Println(newEmployee.Name)

*1 But one thing need to point out is that we’re using the new keyword, we can’t provide the default values to the object properties. When the object is created, Golang provides the default values to the object properties. In the above case, the access to the Name property will return an empty string (“”).

So Any changes made to the input object parameter will be reflected in the original object.

1
2
3
4
5
6
7
8
9
10
11
12
func UpdateEmployee(empDetials *Employee) {
empDetails.Name = "Anshul";
}

var newEmployee = new(Employee)

newEmployee.Name = "Mayank"
newEmployee.Age = 30

UpdateEmployee(newEmployee)

fmt.Println(newEmployee.Name)

In the above case, the object reference is returned from the new keyword. Therefore, while invoking the function, we don’t need to use & to send the reference of the object.

*2 Adding a Function to the Struct
The struct not only defines the properties associated with the object but also represents the behavior of the object. We can add functions to the struct that can add behavior to it. The code to associate a function to the struct is pretty different in Golang.

1
2
3
4
5
6
7
8
9
10
type Employee struct {
Name string
Age int
Designation string
Salary int
}

func (emp Employee) ShowDetails() {
fmt.Println("User Name: ", emp.Name)
}

In the above code, we’re adding a new function that’s bound to the Employee struct. We need to explicitly bind the function to the struct. This function defined can then take the reference of the object created using emp.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
type Employee struct {
Name string
Age int
Designation string
Salary int
}

func (emp Employee) ShowDetails() {
fmt.Println("User Name: ", emp.Name)
}

var newEmployee = new(Employee)
newEmployee.Name = "Mayank"

newEmployee.ShowDetails()