Created
December 9, 2016 00:22
-
-
Save hygull/5470cdd04b084e78451d3360727ec4e4 to your computer and use it in GitHub Desktop.
Pointer and memory allocation for an object with new keyword created by hygull - https://repl.it/Eg23/4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
@Date of creation : 05 Dec 2016. | |
@Aim of program : To allocate storage for data of type int8 using pointer. | |
@Coded by : Rishikesh Agrawani. | |
*/ | |
package main | |
import "fmt" | |
func main() { | |
/*Allocating memory for int8 variable*/ | |
numPtr:=new(int8) | |
fmt.Println(numPtr) //Printing Address | |
*numPtr=10; | |
fmt.Println(numPtr) //Printing Address | |
fmt.Println(*numPtr)//Printing the value stored at the Address | |
/*Allocating memory for user defined variable*/ | |
type Person struct{ | |
Name string | |
Age uint8 | |
Hobbies []string | |
} | |
rishiPtr := new(Person) //Allocating memory for Person object | |
rishiPtr.Name = "Rishikesh Agrawani" //Storing values inside struct using pointer | |
rishiPtr.Age = 24 | |
rishiPtr.Hobbies=[]string{"Programming","Reading books","Watching cricket"} | |
rishi := Person{"Rishikesh Agrawani",24,[]string{"Programming","Reading books","Watching cricket"}} //Creating a separate struct object with same contents | |
fmt.Println(*rishiPtr) //Printing the struct object created using pointer | |
fmt.Println(rishiPtr) //Printing the struct object's address | |
fmt.Println(rishi) //Printing the struct object | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment