Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 241268
Next
Alex Hales
  • 0
Alex HalesTeacher
Asked: August 10, 20222022-08-10T03:21:41+00:00 2022-08-10T03:21:41+00:00In: Go, pointers, reference

pointers – Range references instead values

  • 0

[ad_1]

If you would like to access v‘s reference value (as in for i, v := range arr) to update the value of the object in the array, there are three promising (relatively simple) workarounds:

  1. Like the first answer suggested, update arr[i] instead of v from within the array (e.g., arr[i] = "Hello")

  2. ONLY IF your array contains a set of structs you need to update but not replace in the array’s assignment, set v to arr[i] within your array and then update properties through v (e.g., v := arr[i]; v.thingSheSays = "Hello";)

  3. Or, my favorite—define an array containing the object addresses. Then access the objects using a pointer from within the for-loop. Do this like so:

Input:

a, b, c := "A", "B", "C"
arr := []*string{&a, &b, &c}
fmt.Println("- Arr Value Updates:")
for i, v := range arr {
    *v = "Hello"
    fmt.Println("v's value:      " + *v)
    fmt.Println("arr[i]'s value: " + *arr[i])
}

Output:

- Arr Value Updates:
v's value:      Hello
arr[i]'s value: Hello
v's value:      Hello
arr[i]'s value: Hello
v's value:      Hello
arr[i]'s value: Hello

Hope this was able to help someone, as it initially stumped me as a newbie to golang for-loops. Feel free to share your own methods for avoiding this issue!

[ad_2]

  • 0 0 Answers
  • 14 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 396 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 370 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 361 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.