match: pointers code to videos

This commit is contained in:
Inanc Gumus
2019-07-01 15:30:36 +03:00
parent ac856db508
commit 27ac8cd1e9
2 changed files with 42 additions and 45 deletions

View File

@@ -26,7 +26,7 @@ func main() {
structs()
}
// -----------------------------------------------------------------------------
// ••••••••••••••••••••••••••••••••••••••••••••••••••
type house struct {
name string
@@ -43,7 +43,11 @@ func structs() {
addRoomPtr(&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.rooms: %p\n", &myHouse.rooms)
}
@@ -53,19 +57,11 @@ func addRoom(h house) {
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() {
confused := map[string]int{"one": 2, "two": 1}
fix(confused)
fmt.Println(confused)
// &confused["one"]
@@ -77,7 +73,7 @@ func fix(m map[string]int) {
m["three"] = 3
}
// -----------------------------------------------------------------------------
// ••••••••••••••••••••••••••••••••••••••••••••••••••
func slices() {
dirs := []string{"up", "down", "left", "right"}
@@ -89,29 +85,30 @@ func slices() {
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) {
lv := *list
for i := range lv {
lv[i] = strings.ToUpper(lv[i])
}
*list = append(*list, "HEISEN BUG")
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() {
nums := [...]int{1, 2, 3}
@@ -126,7 +123,6 @@ func arrays() {
func incr(nums [3]int) {
fmt.Printf("incr nums : %p\n", &nums)
for i := range nums {
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) {
fmt.Printf("incrByPtr nums: %p\n", &nums)
for i := range nums {
nums[i]++
// (*nums)[i]++
nums[i]++ // same: (*nums)[i]++
}
}