Thursday 9 May 2019

Go language: Structure pointers


You can store the address of a structure using structure pointer.

Example
emp1 := employee{"Krishna", 1}
var empPtr *employee
empPtr = &emp1

How to access the variables of a structure using pointer?
Use '.' operator to acceess the variables of a structure.

Example
empPtr.name = "Ram"
empPtr.id = 12345

App.java
package main

import "fmt"

func main() {
 emp1 := employee{"Krishna", 1}

 fmt.Println("name : ", emp1.name)
 fmt.Println("id : ", emp1.id)

 var empPtr *employee
 empPtr = &emp1

 empPtr.name = "Ram"
 empPtr.id = 12345

 fmt.Println("\nname : ", emp1.name)
 fmt.Println("id : ", emp1.id)
}

type employee struct {
 name string
 id   int
}

Output
name :  Krishna
id :  1

name :  Ram
id :  12345


Previous                                                 Next                                                 Home

No comments:

Post a Comment