代码拉取完成,页面将自动刷新
package routine
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCloneable(t *testing.T) {
//struct can not be cast to interface
var value any = personCloneable{Id: 1, Name: "Hello"}
_, ok := value.(Cloneable)
assert.False(t, ok)
//pointer can be cast to interface
var pointer any = &personCloneable{Id: 1, Name: "Hello"}
_, ok2 := pointer.(Cloneable)
assert.True(t, ok2)
//nil pointer can be cast to interface
pointer = (*personCloneable)(nil)
cloneable, ok3 := pointer.(Cloneable)
assert.True(t, ok3)
assert.True(t, cloneable != nil)
}
func TestCloneable_Clone(t *testing.T) {
//clone struct
pc := &personCloneable{Id: 1, Name: "Hello"}
assert.NotSame(t, pc, pc.Clone())
assert.Equal(t, *pc, *(pc.Clone().(*personCloneable)))
//copy pointer
pcs := make([]*personCloneable, 1)
pcs[0] = pc
pcs2 := make([]*personCloneable, 1)
copy(pcs2, pcs)
assert.Same(t, pc, pcs2[0])
//clone nil panic
assert.Panics(t, func() {
pc2 := (*personCloneable)(nil)
_ = pc2.Clone()
})
}
type personCloneable struct {
Id int
Name string
}
func (p *personCloneable) Clone() any {
return &personCloneable{Id: p.Id, Name: p.Name}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。