Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use go language to realize array comparison

Shulou Source: shulou.com Published: 2022-05-31 22:06:40 09月22日 Update

The knowledge points of this article "how to use go language to achieve array comparison" are not quite understood by most people, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use go language to achieve array comparison" article.

Compare whether two arrays are equal

If the two arrays are of the same type (including the length of the array and the type of the elements in the array), we can directly judge whether the two arrays are equal by comparing the operators (= = and! =). Only when all the elements of the two arrays are equal, the array is equal, can not compare two different types of arrays, otherwise the program will not be able to complete compilation.

Package mainimport ("fmt") func main () {/ / compare the array arr1: = [3] string {"Hello", "", "PHP"} arr2: = [3] string {"Hello", "PHP"} fmt.Println ("arr1 = = arr2", arr1 = = arr2)}

Output:

Arr1 = = arr2 true

Two arrays containing three elements are defined, and the elements of the array are the same. Then, we compare the two arrays with = =, and the result returns true, that is, the two arrays are equal.

Comparison of different array lengths

The length of the array is different, so you can't compare the array by = and!

Package mainimport ("fmt") func main () {/ / the array length is different, so you cannot compare the array arr1: = [3] string {"Hello", "", "hi"} arr2: = [2] string {"Hello", ""} fmt.Println ("arr1 = = arr2", arr1 = = arr2)}

After the program runs, the console output is as follows:

# command-line-arguments./main.go:9:35: invalid operation: arr1 = = arr2 (mismatched types [3] string and [2] string) the above article is about "how to use go language to achieve array comparison". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about it, please follow the industry information channel.

Tags: Array two content different element length language type same article knowledge program article output three value only most that is the situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL vpn Apple Shulou Information Shulou Technology