golang切片拷贝的实现

 更新时间:2024年10月15日 10:22:37   作者:快刀一哥  
在Golang中,切片的浅拷贝只复制指向对象的指针,而深拷贝则复制数据本身,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

golang切片有浅拷贝和深拷贝的区别

1、深拷贝(Deep Copy)

拷贝的是数据本身,创造一个样的新对象,新创建的对象与原对象不共享内存,新创建的对象在内存中开辟一个新的内存地址,新对象值修改时不会影响原对象值。既然内存地址不同,释放内存地址时,可分别释放。

值类型的数据,默认全部都是深复制,Array、Int、String、Struct、Float,Bool。

2、浅拷贝(Shallow Copy)

拷贝的是数据地址,只复制指向的对象的指针,此时新对象和老对象指向的内存地址是一样的,新对象值修改时老对象也会变化。释放内存地址时,同时释放内存地址。

引用类型的数据,默认全部都是浅复制,Slice,Map。

去若是浅拷贝,原始的切片变化后所有的切片都会变化,无论切片是否在一个函数,还是一个goroutine中,都会发生变化

如下代码为例子

package main

import "fmt"

func main() {
	
	// 创建一个切片
	originalSlice := []int{1, 2, 3, 4, 5}

	// 使用切片[:]操作符创建浅拷贝
	shallowCopy := originalSlice[:]
	shallowCopy2 := make([]int, len(shallowCopy))
	copy(shallowCopy2, originalSlice)

	// 修改原始切片的元素
	originalSlice[0] = 100

	// 输出原始切片和浅拷贝的切片
	fmt.Println("Original Slice:", originalSlice)
	fmt.Println("Shallow Copy:", shallowCopy)
	fmt.Println("Shallow Copy2:", shallowCopy2)
}

如下代码问题困扰了好久,才发现是切片赋值的问题

func (sf *ServerSession) running(ctx context.Context, subcom []uint8) {
	var err error
	var bytesRead int

	defer func() {
		sf.conn.Close()

	}()
	go sf.prehandler()

	raw := make([]byte, tcpAduMaxSize)

	for {
		select {
		case <-ctx.Done():
			err = errors.New("server active close")
			xlog.Errorln(err)
			return
		default:
		}

		err = sf.conn.SetReadDeadline(time.Now().Add(sf.readTimeout))
		if err != nil {
			xlog.Errorln("set read tcp data time deadline error:", err)
		}
		count, err := sf.conn.Read(raw)

		if err != nil {
			if err != io.EOF && err != io.ErrClosedPipe || strings.Contains(err.Error(), "use of closed network connection") {
				return
			}

			if e, ok := err.(net.Error); ok && !e.Temporary() {
				return
			}

			if bytesRead == 0 && err == io.EOF {
				err = fmt.Errorf("remote client closed, %v", err)
				xlog.Errorln(err)
				return
			}
			// cnt >0 do nothing
			// cnt == 0 && err != io.EOF continue do it next
		}
		adu := raw[:count]

		prodata := new(ProcessData)
		prodata.adu = make([]byte, len(adu))
		copy(prodata.adu, adu)
		prodata.count = count
		prodata.subcom = subcom
		Prodatach <- prodata

	}
}

func (sf *ServerSession) prehandler() {
	for {
		select {
		case prodata := <-Prodatach:
			go sf.prerunhandler(prodata)

		}
	}
}
func (sf *ServerSession) prerunhandler(prodata *ProcessData) {
	length := prodata.count
	adu := make([]byte, len(prodata.adu))
	copy(adu, prodata.adu)
	subcom := prodata.subcom
	xlog.Debug("run handler input1 out1 adu [% X] length is:%d\n", adu, length)
	//可能一次读出来多个数据包 增加处理方式 @20240704
	//直接在for循环中使用
	for length > 0 {
		headdata := adu[:gaHeaderSize]
		head1 := new(gadataHeader)
		buf1 := bytes.NewReader(headdata)
		err := binary.Read(buf1, binary.BigEndian, head1)
		if err != nil {
			xlog.Errorln("ga server decode errr:", err)
		}
		xlog.Debugln("pre process packect SN:", head1.SN)
		galength := int(head1.Datalength) + gaHeaderSize

		if galength <= len(adu) {
			adu1 := adu[:galength]
			xlog.Debug("run handler input out adu [% X ],galength is:%d subcom is %d length is:%d\n", adu1, galength, subcom, length)
			sf.runhandler(adu1, galength, subcom)
			adu = adu[galength:]
			length -= galength

		} else {
			xlog.Debug("data length is %d, adu length is:%d\n", head1.Datalength, len(adu))
			break
		}

	}
}
func (sf *ServerSession) runhandler(adu []byte, count int, subcom []uint8) {
	if count < gaHeaderSize {
		return
	}
	xlog.Debug("run handler input adu [% X ],count is:%d subcom is %d", adu, count, subcom)
	// xlog.Debug("recive data [% x] count is % d \n", adu, count)
	head1 := new(gadataHeader)
	data := adu[:gaHeaderSize]
	buf1 := bytes.NewReader(data)
	err := binary.Read(buf1, binary.BigEndian, head1)
	if err != nil {
		xlog.Errorln("ga server decode errr:", err)
	}
	xlog.Debugln("gaserver head sn:", head1.SN)

	if !Find(subcom, head1.Subcomid) {
		xlog.Debug("subcom: %d is not in array %d \n", head1.Subcomid, subcom)
		return
	}
	// check head ProtocolIdentifier
	// if binary.BigEndian.Uint16(adu[2:]) != tcpProtocolIdentifier {
	// 	rdCnt, length = 0, tcpHeaderMbapSize
	// 	continue
	// }
	length := int(head1.Datalength) + gaHeaderSize
	if count == length {
		xlog.Debug("start decode packet SN is:%d subcom is %d data length is:%d\n", head1.SN, head1.Subcomid, head1.Datalength)
		if err = sf.frameHandler(adu); err != nil {
			xlog.Errorln("gaserver decode error:", err)
			return
		}
	} else {
		xlog.Debug("head data length: %d is not right %d \n", head1.Datalength, count)
		///此处经常出现问题
		xlog.Debug("adu data is [% X] \n", adu)
	}
}
func Find(subcoms []uint8, subcom uint8) bool {
	for i := 0; i < len(subcoms); i++ {
		if subcoms[i] == subcom {
			return true
		}
	}
	return false
}

出现非常奇怪的问题切片内容经常莫名奇妙的改变,最后发现是切片赋值的问题

1449[xx] 2024-07-05 16:28:03.428417 run handler input1 out1 adu [00 C4 11 0A 01 A3 00 0A 00 A1 42 79 08 20 1F 75 A0 00
 00 C5 11 06 01 A3 00 18 00 79 00 00 00 00 00 C0 C2 92 00 00 00 8D 42 95 85 C5 00 97 42 92 3D 71 ] length is:50
1450 [xx] 2024-07-05 16:28:03.428496 pre process packect SN: 196

1452[xx] 2024-07-05 16:28:03.448860 run handler input out adu [00 C6 11 40 01 A3 00 09 00 B6 00 05 53 65 76 65 6E 00 ],galength is:18 subcom is [17 41] length is:50
1453[xx] 2024-07-05 16:28:03.448985 run handler input adu [00 C6 11 40 01 A3 00 09 00 B6 00 05 53 65 76 65 6E 00 ],count is:18 subcom is [17 41]1454 [xx] 2024-07-05 16:28:03.449011 gaserver head sn: 198

到此这篇关于golang切片拷贝的实现的文章就介绍到这了,更多相关golang切片拷贝内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Golang实现CronJob(定时任务)的方法详解

    Golang实现CronJob(定时任务)的方法详解

    这篇文章主要为大家详细介绍了Golang如何通过一个单 pod 去实现一个常驻服务,去跑定时任务(CronJob),文中的示例代码讲解详细,需要的可以参考下
    2023-04-04
  • Golang多线程排序实现快速高效地处理大规模数据

    Golang多线程排序实现快速高效地处理大规模数据

    Golang多线程排序是一种快速高效地处理大规模数据的方法,通过使用Golang的协程和通道,可以将排序任务分配到多个线程中并行处理,提高了排序的效率和速度,需要详细了解可以参考下文
    2023-05-05
  • Go语言使用ioutil.ReadAll函数需要注意基本说明

    Go语言使用ioutil.ReadAll函数需要注意基本说明

    这篇文章主要为大家介绍了Go语言使用ioutil.ReadAll函数需要注意基本说明,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • 详解Go并发编程时如何避免发生竞态条件和数据竞争

    详解Go并发编程时如何避免发生竞态条件和数据竞争

    大家都知道,Go是一种支持并发编程的编程语言,但并发编程也是比较复杂和容易出错的。比如本篇分享的问题:竞态条件和数据竞争的问题
    2023-04-04
  • go语言实现银行卡号Luhn校验

    go语言实现银行卡号Luhn校验

    这篇文章主要为大家介绍了go语言Luhn校验测试银行卡号码的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Go语言中GORM存取数组/自定义类型数据

    Go语言中GORM存取数组/自定义类型数据

    在使用gorm时往往默认的数据类型不满足我们的要求,需要使用一些自定义数据类型作为字段类型,下面这篇文章主要给大家介绍了关于Go语言中GORM存取数组/自定义类型数据的相关资料,需要的朋友可以参考下
    2023-01-01
  • Go与C语言的互操作实现

    Go与C语言的互操作实现

    在Go与C语言互操作方面,Go更是提供了强大的支持。尤其是在Go中使用C,你甚至可以直接在Go源文件中编写C代码,本文就详细的介绍一下如何使用,感兴趣的可以了解一下
    2021-12-12
  • Go并发与锁的两种方式该如何提效详解

    Go并发与锁的两种方式该如何提效详解

    如果没有锁,在我们的项目中,可能会存在多个goroutine同时操作一个资源(临界区),这种情况会发生竞态问题(数据竞态),下面这篇文章主要给大家介绍了关于Go并发与锁的两种方式该如何提效的相关资料,需要的朋友可以参考下
    2022-12-12
  • GoLang 中的随机数的示例代码

    GoLang 中的随机数的示例代码

    本篇文章主要介绍了GoLang 中的随机数的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • 解决Golang map range遍历结果不稳定问题

    解决Golang map range遍历结果不稳定问题

    这篇文章主要介绍了解决Golang map range遍历结果不稳定问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12

最新评论