当前位置:网站首页>golang clean a slice
golang clean a slice
2022-06-24 03:37:00 【Great mage yunzhongjun】
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]
边栏推荐
- Tencent cloud launched its new 100g+ cloud server product!! Expect more than 400g+ in the future!
- What aspects does the intelligent identification system include? Is the technology of intelligent identification system mature now?
- How to bypass CDN to get web pages? How many options are available?
- Sorting out of key vulnerabilities identified by CMS in the peripheral management of red team (I)
- 内存泄漏之KOOM
- How to select a server with appropriate configuration when planning to build a live broadcast platform
- How to build glasses website what are the functions of glasses website construction
- TRTC audio quality problem
- What is the principle of intelligent image recognition? What are the applications of intelligent image recognition?
- Is it necessary to buy EIP? Price analysis of EIP
猜你喜欢

浅谈游戏安全 (一)

Sorting out of key vulnerabilities identified by CMS in the peripheral management of red team (I)

Ar 3D map technology

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

Community pycharm installation visual database

在pycharm中pytorch的安装

Get to know MySQL database

ModStartCMS 主题入门开发教程

618大促:手机品牌“神仙打架”,高端市场“谁主沉浮”?
随机推荐
Is it necessary to buy EIP? Price analysis of EIP
What is the maximum bandwidth of EIP? Mbps how to modify the bandwidth of EIP
What does cloud computing elasticity mean? What are its functions?
Differences between EDI and VMI
Why can't the fortress machine open the port? There is a problem with the use of the fortress machine port
What does elastic scaling of cloud computing mean? What are the application scenarios for elastic scaling of cloud computing?
ModStartCMS 企业内容建站系统(支持 Laravel9)v4.2.0
Chapter 6: UART echo case of PS bare metal and FreeRTOS case development
Tens of millions of Android infected with malicious virus and Microsoft disabled a function of Excel | global network security hotspot on October 9
Tencent cloud ASR product -php realizes the authentication request of the extremely fast version of recording file identification
How to bypass CDN to get web pages? How many options are available?
LeetCode 1047. Delete all adjacent duplicates in the string
How to apply for trademark registration? What are the steps?
take the crown! Tencent security won the 2021 national network security week outstanding innovation achievement award
Actual combat | how to use micro build low code to realize tolerance application
Ar 3D map technology
4. go deep into tidb: detailed explanation of the implementation process of the implementation plan
How to register a trademark? What needs to be prepared?
Community pycharm installation visual database
Cloud development RMB 1 purchase activity is in progress