feat(Go-StudyExample): 添加CloneExample,测试go语言的对象内存和深度克隆的实现关于对象克隆可参考文章[知乎-](https://zhuanlan.zhihu.com/p/59125443)、[知乎-](https://zhuanlan.zhihu.com/p/58065429)

This commit is contained in:
huangzj
2020-05-15 09:59:13 +08:00
parent 4feb1f61f2
commit dbda09379d
5 changed files with 213 additions and 1 deletions
+147
View File
@@ -0,0 +1,147 @@
/*
* @Author : huangzj
* @Time : 2020/5/13 17:52
* @Description:测试深拷贝和浅拷贝的代码类
*/
package example
import (
"Go-StudyExample/entity"
"Go-StudyExample/util"
"fmt"
)
func TestClone() {
fmt.Print("\n\n\n")
fmt.Print(" Go语言深克隆的测试:\n\n")
testJustBasicType()
testHasPointType()
fmt.Println("由此可见其实Go语言对于内存的分配,相对对象是独一份,对于对象的基本类型和值类型(结构体),应该是在其内部开辟了内存空间进行存储,而对于" +
"指针类型来说,直接使用它的地址(指针),不对结构体进行复制")
fmt.Println("")
testDeepCloneByEncode()
testDeepCloneByJsonPackage()
}
func testDeepCloneByJsonPackage() {
rule := entity.SynthesisRule{
SynthesisId: "112",
SynthesisNum: 0,
CostItems: nil,
SynthesisItems: nil,
}
//这边必须是指定的类型,不然没办法转回去
rule1, err := util.DeepCopyByJson(rule)
if err == nil && rule1 != &rule {
fmt.Println("")
fmt.Println("通过json包的方式可以实现深度克隆")
}
}
func testDeepCloneByEncode() {
rule := entity.SynthesisRule{
SynthesisId: "112",
SynthesisNum: 0,
CostItems: nil,
SynthesisItems: nil,
}
rule1 := new(entity.SynthesisRule)
err := util.DeepCopyByGob(rule1, rule)
if err != nil {
panic(err)
}
rule1.SynthesisId = "123"
if rule1 != &rule {
fmt.Println("通过序列化的方式可以实现深度克隆")
}
}
func testJustBasicType() {
rule := entity.SynthesisRule{
SynthesisId: "112",
SynthesisNum: 0,
CostItems: nil,
SynthesisItems: nil,
}
rule1 := rule
rule.SynthesisId = "222"
rule.SynthesisItems = make([][]int, 0)
rule.SynthesisItems = append(rule.SynthesisItems, []int{1})
if &rule1 != &rule {
fmt.Println("结构体中不存在指针,直接用 = 是")
fmt.Println("深克隆")
fmt.Println("")
}
}
func testHasPointType() {
cmp := entity.SynthesisRuleCmp{
SynthesisRule: &entity.SynthesisRule{
SynthesisId: "112",
SynthesisNum: 0,
CostItems: nil,
SynthesisItems: nil,
},
RemainingSynthesisTime: 0,
Sort: 0,
}
cmp1 := cmp
cmp.SynthesisId = "123"
if &cmp != &cmp1 && cmp.SynthesisRule == cmp1.SynthesisRule {
fmt.Println("结构体中存在指针,直接用 = 是深克隆")
fmt.Println("但是对于结构体中的指针来说是浅克隆,直接使用地址")
fmt.Println("")
}
}
func testHasPointTypeList() {
cmp := entity.SynthesisRuleCmp{
SynthesisRule: &entity.SynthesisRule{
SynthesisId: "112",
SynthesisNum: 0,
CostItems: nil,
SynthesisItems: nil,
},
RemainingSynthesisTime: 0,
Sort: 0,
}
cmp1 := entity.SynthesisRuleCmp{
SynthesisRule: &entity.SynthesisRule{
SynthesisId: "112111",
SynthesisNum: 0,
CostItems: nil,
SynthesisItems: nil,
},
RemainingSynthesisTime: 0,
Sort: 0,
}
list := make([]entity.SynthesisRuleCmp, 0)
list = append(list, cmp1)
list = append(list, cmp)
list1 := make([]entity.SynthesisRuleCmp, len(list))
cmp.RemainingSynthesisTime = 1000
fmt.Println(cmp1.RemainingSynthesisTime)
copy(list1, list)
list1[0].SynthesisId = "12333"
if list[0].SynthesisId == list1[0].SynthesisId {
fmt.Println("非深度克隆")
}
list2 := list[0:]
list2[0].SynthesisId = "54323"
if list[0].SynthesisId == list2[0].SynthesisId {
fmt.Println("非深度克隆")
}
}