Get the App
SLTechnology News&Howtos  ›  Development  › 

How to define slicing in Go language

Shulou Source: shulou.com Published: 2022-06-01 02:26:55 09月25日 Update

This article mainly explains "how to define the slice of Go language". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to define the slice of Go language".

Go language slicing is an abstraction of arrays.

Go array length can not be changed, in a specific scene such a collection is not suitable, Go provides a flexible, powerful built-in type slice ("dynamic array"), the length of the slice is not fixed compared with the array, you can add elements, when appended may increase the capacity of the slice.

Define slicing

You can declare an array of unspecified sizes to define slices:

Var identifier [] type

The length of the slice does not need to be specified.

Or use the make () function to create a slice:

Var slice1 [] type = make ([] type, len) can also be abbreviated as slice1: = make ([] type, len)

You can also specify the capacity, where capacity is an optional parameter.

Make ([] T, length, capacity)

Where len is the length of the array and also the initial length of the slice.

Thank you for your reading, the above is the content of "how to define the slice of Go language". After the study of this article, I believe you have a deeper understanding of how to define the slice of Go language, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Array language length learning content capacity strength elements functions functions dynamics parameters scene size that is ideas situations articles more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Docker Linux MySQL NVidia