In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail the "Go language append () and copy () function how to use", detailed content, clear steps, details handled properly, I hope that this "Go language append () and copy () function how to use" the article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.
Append () and copy () functions
If we want to increase the capacity of the slice, we must create a new and larger slice and copy the contents of the original slice.
The following code describes the copy method from the copy slice and the append method to append new elements to the slice.
Example
Package main
Import "fmt"
Func main () {
Var numbers [] int
PrintSlice (numbers)
/ * allow empty slices to be appended * /
Numbers = append (numbers, 0)
PrintSlice (numbers)
/ * add an element to the slice * /
Numbers = append (numbers, 1)
PrintSlice (numbers)
/ * add multiple elements at the same time * /
Numbers = append (numbers, 2pm 3pm 4)
PrintSlice (numbers)
/ * creating a slice numbers1 is twice the capacity of the previous slice * /
Numbers1: = make ([] int, len (numbers), (cap (numbers)) * 2)
/ * copy the contents of numbers to numbers1 * /
Copy (numbers1,numbers)
PrintSlice (numbers1)
}
Func printSlice (x [] int) {
Fmt.Printf ("len=%d cap=%d slice=%v\ n", len (x), cap (x), x)
}
The output of the above code execution is as follows:
Len=0 cap=0 slice= [] len=1 cap=1 slice= [0] len=2 cap=2 slice= [0 1] len=5 cap=6 slice= [0 12 3 4] len=5 cap=12 slice= [0 12 3 4] here, this article "how to use the append () and copy () functions of Go language" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.