Q:

Golang program to compare two arrays using equal to (==) operator

0

In this program, we will compare arrays using the equal to (==) operator and print the appropriate message on the console screen.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to compare two arrays using the equal to (==) operator is given below. The given program is compiled and executed successfully.

// Golang program to compare two arrays
// using the equal to (==) operator.

package main

import "fmt"

func main() {
	arr1 := [...]int{1, 2, 3}
	arr2 := [...]int{1, 2, 3}
	arr3 := [...]int{4, 2, 3}

	if arr1 == arr2 {
		fmt.Println("Arr1 and Arr2 have the similar elements")
	} else {
		fmt.Println("Arr1 and Arr2 have not the similar elements")
	}

	if arr1 == arr3 {
		fmt.Println("Arr1 and Arr3 have the similar elements")
	} else {
		fmt.Println("Arr1 and Arr3 have not the similar elements")
	}

	if arr2 == arr3 {
		fmt.Println("Arr2 and Arr3 have the similar elements")
	} else {
		fmt.Println("Arr2 and Arr3 have not the similar elements")
	}
}

Output:

Arr1 and Arr2 have the similar elements
Arr1 and Arr3 have not the similar elements
Arr2 and Arr3 have not the similar elements

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.

In the main() function, we created and initialized three integer arrays. After that perform the comparison between arrays using the equal to (==) operator and print an appropriate message on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now