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
+3
View File
@@ -19,4 +19,7 @@ func main() {
//--------------------------测试json包的转换功能
example.JsonMarshalTest()
example.JsonUnmarshalTest()
example.TestClone()
}
+20
View File
@@ -0,0 +1,20 @@
/*
* @Author : huangzj
* @Time : 2020/5/15 9:54
* @Description
*/
package entity
type SynthesisRuleCmp struct {
*SynthesisRule
RemainingSynthesisTime int
Sort int
}
type SynthesisRule struct {
SynthesisId string `json:"SynthesisId"`
SynthesisNum int `json:"SynthesisNum"`
CostItems [][]int `json:"CostItems"`
SynthesisItems [][]int `json:"SynthesisItems"`
}
+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("非深度克隆")
}
}
+4 -1
View File
@@ -2,4 +2,7 @@
2020/4/27 :创建项目,添加使用示例如下:
map与struct之间转换的处理工具使用示例,参考代码地址:https://github.com/mitchellh/mapstructure
json与struct之间转换处理工具使用示例
json与struct之间转换处理工具使用示例
2020/5/15 : 添加CloneExample,测试go语言的对象内存和深度克隆的实现关于对象克隆可参考文章[知乎-](https://zhuanlan.zhihu.com/p/59125443)、[知乎-](https://zhuanlan.zhihu.com/p/58065429)
+39
View File
@@ -0,0 +1,39 @@
/*
* @Author : huangzj
* @Time : 2020/5/14 17:53
* @Description
*/
package util
import (
"Go-StudyExample/entity"
"bytes"
"encoding/gob"
"encoding/json"
)
/*
* @param dst 目标对象
* @param src 源对象
* 通过序列化的方式进行克隆
*/
func DeepCopyByGob(dst, src interface{}) error {
var buffer bytes.Buffer
if err := gob.NewEncoder(&buffer).Encode(src); err != nil {
return err
}
return gob.NewDecoder(&buffer).Decode(dst)
}
func DeepCopyByJson(src interface{}) (*entity.SynthesisRule, error) {
var dst = new(entity.SynthesisRule)
b, err := json.Marshal(src)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, dst)
return dst, err
}