match: pointers code to videos
This commit is contained in:
@ -16,44 +16,45 @@ func main() {
|
|||||||
P *int
|
P *int
|
||||||
)
|
)
|
||||||
|
|
||||||
if P == nil {
|
|
||||||
fmt.Printf("P is nil and its address is %p\n", P)
|
|
||||||
}
|
|
||||||
|
|
||||||
counter = 100 // counter is an int variable
|
counter = 100 // counter is an int variable
|
||||||
P = &counter // P is a pointer int variable
|
P = &counter // P is a pointer int variable
|
||||||
V = *P // V is a int variable (a copy of counter)
|
V = *P // V is a int variable (a copy of counter)
|
||||||
|
|
||||||
if P == &counter {
|
if P == nil {
|
||||||
fmt.Printf("P's address is equal to counter: %p == %p\n", P, &counter)
|
fmt.Printf("P is nil and its address is %p\n", P)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("counter : %-16d address: %-16p\n", counter, &counter)
|
if P == &counter {
|
||||||
fmt.Printf("P : %-16p address: %-16p *P: %-16d\n", P, &P, *P)
|
fmt.Printf("P is equal to counter's address: %p == %p\n",
|
||||||
fmt.Printf("V : %-16d address: %-16p\n", V, &V)
|
P, &counter)
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println("\n••••• CHANGE: counter")
|
fmt.Printf("counter: %-13d addr: %-13p\n", counter, &counter)
|
||||||
|
fmt.Printf("P : %-13p addr: %-13p *P: %-13d\n", P, &P, *P)
|
||||||
|
fmt.Printf("V : %-13d addr: %-13p\n", V, &V)
|
||||||
|
|
||||||
|
fmt.Println("\n••••• change counter")
|
||||||
counter = 10 // V doesn't change because it's a copy
|
counter = 10 // V doesn't change because it's a copy
|
||||||
fmt.Printf("counter : %-16d address: %-16p\n", counter, &counter)
|
fmt.Printf("counter: %-13d addr: %-13p\n", counter, &counter)
|
||||||
fmt.Printf("V : %-16d address: %-16p\n", V, &V)
|
fmt.Printf("V : %-13d addr: %-13p\n", V, &V)
|
||||||
|
|
||||||
fmt.Println("\n••••• CHANGE IN: passVal")
|
fmt.Println("\n••••• change counter in passVal()")
|
||||||
counter = passVal(counter)
|
counter = passVal(counter)
|
||||||
fmt.Printf("counter : %-16d address: %-16p\n", counter, &counter)
|
fmt.Printf("counter: %-13d addr: %-13p\n", counter, &counter)
|
||||||
|
|
||||||
fmt.Println("\n••••• CHANGE IN: passPtrVal")
|
fmt.Println("\n••••• change counter in passPtrVal()")
|
||||||
passPtrVal(&counter) // same as passPtrVal(&counter) (no need to return)
|
passPtrVal(&counter) // same as passPtrVal(&counter) (no need to return)
|
||||||
passPtrVal(&counter) // same as passPtrVal(&counter) (no need to return)
|
passPtrVal(&counter) // same as passPtrVal(&counter) (no need to return)
|
||||||
fmt.Printf("counter : %-16d address: %-16p\n", counter, &counter)
|
fmt.Printf("counter: %-13d addr: %-13p\n", counter, &counter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// *pn is a int pointer variable (copy of P)
|
// *pn is a int pointer variable (copy of P)
|
||||||
func passPtrVal(pn *int) {
|
func passPtrVal(pn *int) {
|
||||||
fmt.Printf("pn : %-16p address: %-16p *pn: %d\n", pn, &pn, *pn)
|
fmt.Printf("pn : %-13p addr: %-13p *pn: %d\n", pn, &pn, *pn)
|
||||||
|
|
||||||
// pointers can breach function isolation borders
|
// pointers can breach function isolation borders
|
||||||
*pn++ // counter changes because `pn` points to `counter` — (*pn)++
|
*pn++ // counter changes because `pn` points to `counter` — (*pn)++
|
||||||
fmt.Printf("pn : %-16p address: %-16p *pn: %d\n", pn, &pn, *pn)
|
fmt.Printf("pn : %-13p addr: %-13p *pn: %d\n", pn, &pn, *pn)
|
||||||
|
|
||||||
// setting it to nil doesn't effect the caller (the main function)
|
// setting it to nil doesn't effect the caller (the main function)
|
||||||
pn = nil
|
pn = nil
|
||||||
@ -62,6 +63,6 @@ func passPtrVal(pn *int) {
|
|||||||
// n is a int variable (copy of counter)
|
// n is a int variable (copy of counter)
|
||||||
func passVal(n int) int {
|
func passVal(n int) int {
|
||||||
n = 50 // counter doesn't change because `n` is a copy
|
n = 50 // counter doesn't change because `n` is a copy
|
||||||
fmt.Printf("n : %-16d address: %-16p\n", n, &n)
|
fmt.Printf("n : %-13d addr: %-13p\n", n, &n)
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ func main() {
|
|||||||
structs()
|
structs()
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// ••••••••••••••••••••••••••••••••••••••••••••••••••
|
||||||
|
|
||||||
type house struct {
|
type house struct {
|
||||||
name string
|
name string
|
||||||
@ -43,7 +43,11 @@ func structs() {
|
|||||||
|
|
||||||
addRoomPtr(&myHouse)
|
addRoomPtr(&myHouse)
|
||||||
fmt.Printf("structs() : %p %+v\n", &myHouse, myHouse)
|
fmt.Printf("structs() : %p %+v\n", &myHouse, myHouse)
|
||||||
|
}
|
||||||
|
|
||||||
|
func addRoomPtr(h *house) {
|
||||||
|
h.rooms++ // same: (*h).rooms++
|
||||||
|
fmt.Printf("addRoomPtr() : %p %+v\n", h, h)
|
||||||
fmt.Printf("&myHouse.name : %p\n", &myHouse.name)
|
fmt.Printf("&myHouse.name : %p\n", &myHouse.name)
|
||||||
fmt.Printf("&myHouse.rooms: %p\n", &myHouse.rooms)
|
fmt.Printf("&myHouse.rooms: %p\n", &myHouse.rooms)
|
||||||
}
|
}
|
||||||
@ -53,19 +57,11 @@ func addRoom(h house) {
|
|||||||
fmt.Printf("addRoom() : %p %+v\n", &h, h)
|
fmt.Printf("addRoom() : %p %+v\n", &h, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
func addRoomPtr(h *house) {
|
// ••••••••••••••••••••••••••••••••••••••••••••••••••
|
||||||
// (*h).rooms++
|
|
||||||
h.rooms++
|
|
||||||
fmt.Printf("addRoomPtr() : %p %+v\n", h, h)
|
|
||||||
}
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
func maps() {
|
func maps() {
|
||||||
confused := map[string]int{"one": 2, "two": 1}
|
confused := map[string]int{"one": 2, "two": 1}
|
||||||
|
|
||||||
fix(confused)
|
fix(confused)
|
||||||
|
|
||||||
fmt.Println(confused)
|
fmt.Println(confused)
|
||||||
|
|
||||||
// &confused["one"]
|
// &confused["one"]
|
||||||
@ -77,7 +73,7 @@ func fix(m map[string]int) {
|
|||||||
m["three"] = 3
|
m["three"] = 3
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// ••••••••••••••••••••••••••••••••••••••••••••••••••
|
||||||
|
|
||||||
func slices() {
|
func slices() {
|
||||||
dirs := []string{"up", "down", "left", "right"}
|
dirs := []string{"up", "down", "left", "right"}
|
||||||
@ -89,29 +85,30 @@ func slices() {
|
|||||||
fmt.Printf("slices list : %p %q\n", &dirs, dirs)
|
fmt.Printf("slices list : %p %q\n", &dirs, dirs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func up(list []string) {
|
|
||||||
for i := range list {
|
|
||||||
list[i] = strings.ToUpper(list[i])
|
|
||||||
fmt.Printf("up.list[%d] : %p\n", i, &list[i])
|
|
||||||
}
|
|
||||||
// *list = append(*list, "HEISEN BUG")
|
|
||||||
list = append(list, "HEISEN BUG")
|
|
||||||
|
|
||||||
fmt.Printf("up list : %p %q\n", &list, list)
|
|
||||||
}
|
|
||||||
|
|
||||||
func upPtr(list *[]string) {
|
func upPtr(list *[]string) {
|
||||||
lv := *list
|
lv := *list
|
||||||
|
|
||||||
for i := range lv {
|
for i := range lv {
|
||||||
lv[i] = strings.ToUpper(lv[i])
|
lv[i] = strings.ToUpper(lv[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
*list = append(*list, "HEISEN BUG")
|
*list = append(*list, "HEISEN BUG")
|
||||||
|
|
||||||
fmt.Printf("upPtr list : %p %q\n", list, list)
|
fmt.Printf("upPtr list : %p %q\n", list, list)
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
func up(list []string) {
|
||||||
|
for i := range list {
|
||||||
|
list[i] = strings.ToUpper(list[i])
|
||||||
|
fmt.Printf("up.list[%d] : %p\n", i, &list[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
list = append(list, "HEISEN BUG")
|
||||||
|
|
||||||
|
fmt.Printf("up list : %p %q\n", &list, list)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ••••••••••••••••••••••••••••••••••••••••••••••••••
|
||||||
|
|
||||||
func arrays() {
|
func arrays() {
|
||||||
nums := [...]int{1, 2, 3}
|
nums := [...]int{1, 2, 3}
|
||||||
@ -126,7 +123,6 @@ func arrays() {
|
|||||||
|
|
||||||
func incr(nums [3]int) {
|
func incr(nums [3]int) {
|
||||||
fmt.Printf("incr nums : %p\n", &nums)
|
fmt.Printf("incr nums : %p\n", &nums)
|
||||||
|
|
||||||
for i := range nums {
|
for i := range nums {
|
||||||
nums[i]++
|
nums[i]++
|
||||||
fmt.Printf("incr.nums[%d] : %p\n", i, &nums[i])
|
fmt.Printf("incr.nums[%d] : %p\n", i, &nums[i])
|
||||||
@ -134,8 +130,8 @@ func incr(nums [3]int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func incrByPtr(nums *[3]int) {
|
func incrByPtr(nums *[3]int) {
|
||||||
|
fmt.Printf("incrByPtr nums: %p\n", &nums)
|
||||||
for i := range nums {
|
for i := range nums {
|
||||||
nums[i]++
|
nums[i]++ // same: (*nums)[i]++
|
||||||
// (*nums)[i]++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user