当前位置:网站首页>golang clean a slice
golang clean a slice
2022-06-24 03:34:00 【大魔法师云中君】
To remove all elements, set the slice to nil:
a := []string{
"A", "B", "C", "D", "E"}
a = nil
fmt.Println(a, len(a), cap(a)) // [] 0 0
This will release the underlying array to the garbage collector
(assuming there are no other references).
Keep allocated memory
To keep the underlying array, slice the slice to zero length:
a := []string{
"A", "B", "C", "D", "E"}
a = a[:0]
fmt.Println(a, len(a), cap(a)) // [] 0 5
If the slice is extended again, the original data reappears:
fmt.Println(a[:2]) // [A B]
边栏推荐
- Troubleshooting and resolution of errors in easycvr calling batch deletion interface
- Chapter 5: key led demo case of PS bare metal and FreeRTOS case development
- [competition experience sharing] design of intelligent guide rod
- Ar 3D map technology
- How to select a server with appropriate configuration when planning to build a live broadcast platform
- What does cloud desktop mean? What are the characteristics of cloud desktop?
- [congratulations] rock solid! A new generation of AMD Blackstone architecture instance is launched!
- Implement the throttling de dithering function
- What is fortress resource authorization? What is barrier machine?
- Chapter 4: LED flash case of PS bare metal and FreeRTOS case development
猜你喜欢

老弹出explorer.exe遇到问题已停止工作,怎么办?

元气森林推“有矿”,农夫山泉们跟着“卷”?

内存泄漏之KOOM

【代码随想录-动态规划】T392.判断子序列

QT creator tips
Thank you for your recognition! One thank-you note after another

在pycharm中pytorch的安装

On Sunday, I rolled up the uni app "uview excellent UI framework"

Community pycharm installation visual database

Sorting out of key vulnerabilities identified by CMS in the peripheral management of red team (I)
随机推荐
Mocktio usage (Part 2)
Typera cooperates with picgo to upload pictures to its own server with one click and obtain external links at the same time
What does elastic scaling of cloud computing mean? What are the application scenarios for elastic scaling of cloud computing?
Sorting out of key vulnerabilities identified by CMS in the peripheral management of red team (I)
What protocols do fortress computers have and what protocols do fortress computers generally use
Is it necessary to buy EIP? Price analysis of EIP
If the cloud knows that security is important
How to register a trademark? What needs to be prepared?
What technology does cloud computing elasticity scale? What are the advantages of elastic scaling in cloud computing?
RI Geng series: tricks of using function pointers
老弹出explorer.exe遇到问题已停止工作,怎么办?
What is the difference between elasticity and scalability of cloud computing? What does elastic scaling of cloud computing mean?
在pycharm中pytorch的安装
元气森林推“有矿”,农夫山泉们跟着“卷”?
Applicationclientprotocol of yarn source code
Grpc: how to implement the restful API for file uploading?
The importance of the computer room to the stable operation of the server
Chapter 5: key led demo case of PS bare metal and FreeRTOS case development
Grpc: how do I start multiple ports?
Several key tools for cloud native implementation