Skip to content

Instantly share code, notes, and snippets.

@dakshgautam1
Created October 14, 2023 01:50
Show Gist options
  • Save dakshgautam1/3afa13aa87673a3ff934a7c512517704 to your computer and use it in GitHub Desktop.
Save dakshgautam1/3afa13aa87673a3ff934a7c512517704 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
type Node struct {
name string
age int
}
type MNode struct {
name string
age int
alphas []rune
}
func main() {
var yo int = 34
fmt.Printf("actual number - %d with address %p\n", yo, &yo) // actual number - 34 with address 0x14000112018
fmt.Printf("---------------------------------\n")
var str string = "Mike"
fmt.Printf("actual string %s with address %p\n", str, &str) // actual string Mike with address 0x14000104020
fmt.Printf("---------------------------------\n")
var arr [2]int
arr[0] = 1999
arr[1] = 888
// array's address = first element of array's address
fmt.Printf("arr %v with array address %p - \n first element address - %p, \n second element address - %p\n", arr, &arr, &arr[0], &arr[1])
/*
output:
arr [1999 888] with array address 0x14000112030 -
first element address - 0x14000112030,
second element address - 0x14000112038
*/
fmt.Printf("---------------------------------\n")
var node Node = Node{name: "Mike Perry", age: 29}
/*
- think of struct as an array.
- address of struct = address of first property of the struct
*/
fmt.Printf("Node %v with address %p \n [name: %d name's address: %p], \n [age: %d age's address: %p] \n", node, &node, node.name, &node.name, node.age, &node.age)
fmt.Printf("---------------------------------\n")
var nameList []string
nameList = append(nameList, "Mike")
nameList = append(nameList, "Perry")
/*
- in this case address of nameList is not equal to address of first element of the name list.
- the reason for that any slice variables stores the pointer to the type.
*/
fmt.Printf("slice value: %v slices's address; %p \n [first element: %s, first el address: %p]\n [second element: %s, second el address: %p] \n",
nameList,
&nameList,
nameList[0],
&nameList[0],
nameList[1],
&nameList[1],
)
fmt.Printf("---------------------------------\n")
var nodeList []Node
nodeList = append(nodeList, Node{name: "Tom1", age: 20})
nodeList = append(nodeList, Node{name: "Tom2", age: 22})
fmt.Printf("slice of objects value: %v slices's address; %p \n [first element: %v, first el address: %p]\n [second element: %v, second el address: %p] \n",
nodeList,
&nodeList,
nodeList[0],
&nodeList[0],
nodeList[1],
&nodeList[1],
)
fmt.Printf("---------------------------------\n")
var newNodeList []MNode
newNodeList = append(newNodeList, MNode{name: "Tom1", age: 20, alphas: []rune("HELLO1")})
// newNodeList = append(newNodeList, MNode{name: "Tom2", age: 22, alphas: []rune("HELLO1")})
fmt.Printf("NewNodeList %v, NewNodeListAddress: %p \n NewNodeValue %v, NewNodeValueAddress: %p \n [NewNode name: %s, NewNode name address: %p] \n [NewNode age: %s, NewNode age address: %d], \n [NewNode rune value %v, NewNode rune address --%p]\n [rune valus --> %c %p]\n",
newNodeList,
&newNodeList,
newNodeList[0],
&(newNodeList[0]), // here...
newNodeList[0].name,
&(newNodeList[0].name), // here...
newNodeList[0].age,
&(newNodeList[0].age),
newNodeList[0].alphas,
&(newNodeList[0].alphas),
newNodeList[0].alphas[0],
&(newNodeList[0].alphas[0]),
)
fmt.Printf("---------------------------------\n")
fmt.Printf("newNodeList ptr - %p & newNodeList first el ptr %p\n", &newNodeList, &newNodeList[0])
fmt.Printf("newNodeList[0] ptr - %p &newNodeList[0].name %p\n are same", &(newNodeList[0]), &(newNodeList[0].name))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment