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 a 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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6065585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:24:25+00:00 2026-05-23T09:24:25+00:00

Using a setter function for a struct, but not working as anticipated: package main

  • 0

Using a setter function for a struct, but not working as anticipated:

package main

import "fmt"

type T struct { Val string }

// this setter seems not to work
func (t T) SetVal( s string ) {
        t.Val = s
}

// this setter, using ptr to T, seems to work ok
func (t *T) SetVal2( s string ) {
        (*t).Val = s
}

func main() {
        v := T{"abc"}
        fmt.Println( v )        // prints {abc}
        v.SetVal("pdq")
        fmt.Println( v )        // prints {abc}, was expecting {pdq}!
        v.SetVal2("xyz")
        fmt.Println( v )        // prints {xyz}!
}

I am missing some fundamental understanding – why doesn’t SetVal work?

the behavior is similar to setting values in reflect which only works if provided a pointer to the object as versus the object itself

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:24:25+00:00Added an answer on May 23, 2026 at 9:24 am

    Here is the fundamental understanding you are missing: when a struct is passed in to a function as a pointer, the function can modify the original struct because it has a pointer to it. However, when a struct is passed in to a function via its value, then a NEW copy of the struct is actually created just for that function call, and any modifications to this new copy of the struct will not affect the original struct.

    You can prove that this is the way it works by printing out the actual addresses of the structs in question:

    package main
    
    import "fmt"
    
    type T struct { Val string }
    
    func (t T) SetVal( s string ) {
            fmt.Printf("Address of copy is %p\n", &t);
    }
    
    func (t *T) SetVal2( s string ) {
            fmt.Printf("Pointer argument is %p\n", t);
    }
    
    func main() {
            v := T{"abc"}
            fmt.Printf("Address of v is %p\n", &v);
            v.SetVal("pdq")
            v.SetVal2("xyz")
    }
    

    The code above results in this output when I run it in the Go playground:

    Address of v is 0xf840001290
    Address of copy is 0xf8400013e0
    Pointer argument is 0xf840001290
    

    Notice how the first and third pointer printed out are equal, which means they are the same struct. But the second pointer is different because it is a copy.

    This seems to be exactly the same way that C structs / function parameters work, by the way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Convention question: When do you use a Getter/Setter function rather than using
Is there a benifit to using: private var _someProp:String; public function set someProp(value:String):void {
Im using the Entity Framework and I have a rowversion (timestamp) field on a
I'm new to Java, but have some OOP experience with ActionScript 3, so I'm
I have been reading Exceptional C++ by Herb Sutter . On reaching Item 32
I have a colleague in my company whose opinions I have a great deal
Consider the sample application below. It demonstrates what I would call a flawed class
Consider the following proxy class: class VertexProxy { public: VertexProxy(double* x, double* y, double*
I have a flashlite3 application with navigation consisting of icons the user can browse
I have something like this: class X(): def __init__(self): self.__name = None def _process_value(self,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.