Skip to content

Struct values not being cloned when assigned from/called with when variable type is interface #661

Open
@myitcv

Description

@myitcv

(had a look, couldn't recall this having been discussed or raised before)

Consider the following code:

package main

import (
	"fmt"
)

type S struct {
	Name string
}

func (s S) SetName(n string) {
	s.Name = n
}

type NameSet interface {
	SetName(n string)
}

func main() {
	s1 := S{}
	var s2 NameSet = s1
	s1.Name = "Rob"
	
	fmt.Printf("s1: %v, s2: %v\n", s1, s2)
	
	s2.SetName("Pike")
	fmt.Printf("s1: %v, s2: %v\n", s1, s2)
}

Per the Go Playground, this should output:

s1: {Rob}, s2: {}
s1: {Rob}, s2: {}

But per the GopherJS Playground we get:

s1: {Rob}, s2: {Rob} 
s1: {Pike}, s2: {Pike} 

Reason being the assignment to s2 is not $clone-ing the value from s1.

Similarly, in the call s2.SetName we are not $clone-ing.

If the type of s2 is changed to S then everything works as expected.

Will look into this a bit more later, just logging here for now.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions