Skip to content

Instantly share code, notes, and snippets.

@hygull
Created December 9, 2016 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hygull/5470cdd04b084e78451d3360727ec4e4 to your computer and use it in GitHub Desktop.
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
/*
@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