Hello all, 

I can not understand why when we use append, it does not rewrite values in 
the same address in the memory that it had before, 

(I'm not sure it "append" problem or the variable |  or just my problem for 
sure :) ) 

let me explain what I mean, 

I solve a question in LeetCode (189. Rotate Array 
<https://leetcode.com/problems/rotate-array/>). The question said we give 
you an array, and you should rotate the array to the right by k steps, 
where k is non-negative.

I thought, so it's easy, we can do something like this :

```
func rotate(nums []int, k int) {
       nums = append(nums[len(nums) - k:], nums[:len(nums) - k]...)
}
```
When I used `fmt.Println` I got the right value; it was fine, but while 
running the test case, I got the same value as before, and it says so you 
didn't change anything there is still the same value as before, and it was 
completely wrong, 

after that, I rechecked my code; I did something like this :

```
func rotate(nums []int, k int) {

      for i := range nums{
            fmt.Println(&nums[i], nums[i])
      }

     nums = append(nums[len(nums) - k:], nums[:len(nums) - k]...)

     fmt.Println("-----------------------------------------")
     for i := range nums{
           fmt.Println(&nums[i], nums[i])
      }
}
```
the result was like this :

[image: Screenshot 2021-08-06 115209.png]

I saw all addresses in memory was changed, 

now my question is why it doesn't rewrite the values in the same address as 
before?

Could you please describe to me what's going on in memory, why it happened, 
and why it couldn't rewrite values in the same location in the memory? 

I don't have strong knowledge of what's going on in memory in Golang or at 
all. If there are any resources out there, could you please share them with 
me?

Thanks again, 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/0c64f322-f55d-4063-9bb8-ca6f67d4fbccn%40googlegroups.com.

Reply via email to