Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between slice and arry in Golang

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about the difference between slice and arry in Golang. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

First, the question is that after the slice is copied, the slice content after the copy is modified, and as a result, the slice content before the copy also changes.

Package main

Import ("fmt"reflect")

Func main () {var arr [8] int = [8] int {1} var arr1 [8] int var slice, slice1 [] int fmt.Println ("type of:", reflect.TypeOf (arr), reflect.TypeOf (slice)) arr1 = arr fmt.Println ("1: arr | arr1 of:", arr, arr1) arr1 = [8] int {3} fmt.Println ("2: arr | arr1 of:", arr, arr1) slice = append (slice 2) slice1 = slice fmt.Println ("3: slice | slice1 of:", slice, slice1) slice1 [0] = 5 fmt.Println ("4: slice | slice1 of:", slice Slice1)} output:type of: [8] int [] int1: arr | arr1 of: [10000000] [10000000] 2: arr | arr1 of: [10000000000] [3000000000] / / it's just that the modified one has changed 3: slice | slice1 of: [2] [2] 4: slice | slice1 of: [5] [5] / / that's weird. Why has everything changed?

Result analysis:

Seeing the output result, the author is blind at first, how can there be two results in the replication of two kinds of [8] int, [] int? After the replication of arry, the two arry are independent, but after the replication of slice, the content will change at the same time.

Second, the cause of the problem

Before we begin, let's take a look at what is slice and what is arry?

Example:

Var arr [8] int / / this specified length array is of type arry, and copying is a copy of the value.

Var slice [] int / / this is the slice type, more like a pointer, and the copy operation is actually the public address of the operation pointer.

Arry and slice are regarded as two data types in Go, one is that they have the same characteristics as basic types such as int, and the other is that reference types have the same characteristics as pointers and interface. It is this difference that leads to the operation in the above code, and the output will be different.

The above is the difference between slice and arry in Golang shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report