In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the "Go language basic slice creation and initialization example analysis". In the daily operation, I believe that many people have doubts about the creation and initialization example analysis of GE language basic slices. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "the creation and initialization example analysis of Go language basic slices". Next, please follow the editor to study!
Overview
Slicing is a dynamic array
Automatically resize as needed
Compared with arrays, the length of slices can be modified at run time
Syntax 1. Create and initialize slice make
Create a slice using the built-in function make ():
Var slice [] type = make ([] type, len, cap) / / abbreviation: slice: = make ([] type, len, cap) literal quantity
Var variable name [] type
Slice1:= [] string {"Zhang San", "Li Si"} / / string slices with 5 elements in length and capacity slice2: = [] int {10, 20, 30} / / Integer slices with 3 elements in length and capacity 2. Use slice assignment and slice
You can change the behavior of an element using the [] operator, as an example:
/ / create an integer slice / / its capacity and length are all 5 elements slice1:= [] string {"Zhang San", "Li Si", "Wang Wu", "Ma Liu", "Lao Qi"} / / change the value of the element with index 1 slice1 [1] = "Xiao Zhang San"
Create a slice using a slice
/ / create an integer slice / / its length and capacity are all 5 elements slice1:= [] string {"Zhang San", "Li Si", "Wang Wu", "Ma Liu", "Lao Qi"} / / create a new slice / / its length is 3 elements and its capacity is 3 elements newSlice:=slice1 [2:5] slice growth
Use append to add elements to a slice while increasing the length and capacity of the slice
/ / create an integer slice / / its length and capacity are all 5 elements slice1:= [] string {"Zhang San", "Li Si", "Wang Wu", "Ma Liu", "Lao Qi"} / / use the original capacity to allocate a new element / / assign the new element to 60newSlice:=append (slice1, "I'm new") traversal slice
Iterative slicing using for range
Slice1: = [] string {"Zhang San", "Li Si", "Wang Wu", "Ma Liu", "Lao Qi"} for k, v: = range slice1 {fmt.Println (k, v)}
Iterate slicing with for Loop
Slice1: = [] string {"Zhang San", "Li Si", "Wang Wu", "Ma Liu", "Lao Qi"} for I: = 0; I
< len(slice1); i++ { fmt.Println(i,slice1[i])}总结 slice 的默认开始位置是0,ar[:n]等价于ar[0:n] slice是引用类型,是一个指向数组的指针 不能使用 == 来判断两个slice 是给含有全部相同元素 >To determine whether slice is empty, use len (s) = = 0 instead of s = = nil
General example package mainimport ("fmt") func main () {/ / 1, create slice var slice [] int = make ([] int, 3) fmt.Println (slice) slice1: = [] string {"Zhang San", "Li Si", "Wang Wu", "Ma Liu", "Lao Qi"} fmt.Println (slice1) slice2: = [] int {10,20 30} fmt.Println (slice2) / / II Use slice / / use the [] operator to change an element slice1 [1] = "Xiao Zhang San" fmt.Println (slice1) / / create a slice using slice [subscript is from 0] newSlice: = slice1 [0:2] fmt.Println (newSlice) / / slice growth newSlice = append (slice1) "I am new") fmt.Println (newSlice) / / use for range iterative slices [k: for subscript, v for value] for k, v: = range slice1 {fmt.Println (k, v)} / / use for loop to iterate slices for I: = 0 I
< len(slice1); i++ { fmt.Println(i,slice1[i]) }}示例一 两个slice是否相等package mainimport ( "fmt" "reflect")func main() { //两个slice是否相等 slice1 := []string{"张三", "李四", "王五", "马六"} slice2 := []string{"张三", "李四", "王五", "马六"} if reflect.DeepEqual(slice1, slice2) { fmt.Println("两个slice相等") } else { fmt.Println("两个slice不相等") }}示例二 两个数字是否包含package mainimport ( "fmt" "sort" "strings")func main() { slice1 := []string{"张三", "李四", "王五", "马六", "老七"} fmt.Println(slice1) target := "李四" i := sort.Search(len(slice1), func(i int) bool { return slice1[i] >= target}) if strings.EqualFold (slice1 [I], target) {fmt.Println (target, "exist, its subscript is", I)} else {fmt.Println ("does not exist", target)}} so far, the study on "sample analysis of the creation and initialization of Go language basic slices" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.