It's because your method receivers are values, not pointers:

func (b Builder) setFirst() {
    b.firstElement = "First element"
}

In Go, method receivers work a lot like regular function arguments. When
you call the above method, you're operating on a copy of the Builder
struct. Any time you pass an argument by value like this, its value is
copied. What you probably want is to define the method on a pointer
receiver:

func (b *Builder) setFirst() {
    b.firstElement = "First element"
}

Now, when you call this method on an object, only the pointer is copied.
Inside the method, you're referring to the same Builder instance that you
are when calling the method.

On Sun, Dec 5, 2021 at 10:47 PM Денис Мухортов <muhortovdeni...@gmail.com>
wrote:

> I have 2 empty strings in the output. But why?
> https://play.golang.com/p/v7zEVBM17YH
>
> --
> 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/4060a981-f43c-49ec-92f7-849ab23d1ea9n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/4060a981-f43c-49ec-92f7-849ab23d1ea9n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CAA%3DXfu1Vj2k8-CewHNQmQ%2B3y%3D4cX_Y6w0GORhNmjXTnpWcAfzw%40mail.gmail.com.

Reply via email to