English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

고대롬 기본 강의

고대롬 제어문

고대롬 함수 & 메서드

고대롬 구조체

고대롬 슬라이스 & 배열

고대롬 문자열(String)

고대롬 포인터

고대롬 인터페이스

Golang 병목

Golang 예외(Error)

Golang 다른 항목

Go 언어 슬라이스 비교

Go 언어에서는, 슬라이스는 배열보다 더 강력하고 유연하며 편리하며 가벼운 데이터 구조입니다. 슬라이스는 동일한 타입의 요소를 저장하는 변형 가능 길이의 시퀀스이며, 동일한 슬라이스에 다른 타입의 요소를 저장할 수 없습니다. Go 슬라이스에서는 다음을 사용할 수 있습니다Compare()함수는 두 바이트 타입의 슬라이스를 상호 비교합니다비교。이 함수는 정수 값을 반환하며, 이 값은 이 슬라이스들이 일치하거나 불일치하는지를 나타냅니다. 이 값은 다음과 같습니다:

  • 결과가 0이면 slice_1 == slice_2。

  • 결과가-1,면 slice_1 <slice_2。

  • 결과가+1,면 slice_1> slice_2。

이 함수는 bytes 패키지에 정의되어 있으며, 따라서 Compare 함수에 접근하기 위해 bytes 패키지를 프로그램에 가져오셔야 합니다.

문법:

func Compare(slice_1, slice_2 []byte) int

이 개념에 대해 예제를 통해 논의해 보겠습니다:

//바이트 스ライ스 비교
package main
import (
    "bytes"
    "fmt"
)
func main() {
    //단축 선언 사용하여 생성 및 초기화 바이트 스ライ스
    slice_1 := []byte{'G', 'E', 'E', 'K', 'S'}
    slice_2 := []byte{'G', 'E', 'e', 'K', 'S'}
    //스ライ스 비교
    //Compare 함수 사용
    res := bytes.Compare(slice_1, slice_2)
    if res == 0 {
        fmt.Println("!..스ライ스 일치..!")
    } else {
        fmt.Println("!..스ライ스 불일치..!")
    }
}

출력:

!..스ライ스 불일치..!

두 바이트 스ライ스 비교 예제:

package main
import (
    "bytes"
    "fmt"
)
func main() {
    slice_1 := []byte{'A', 'N', 'M', 'O', 'P', 'Q'}
    slice_2 := []byte{'a', 'g', 't', 'e', 'q', 'm'}
    slice_3 := []byte{'A', 'N', 'M', 'O', 'P', 'Q'}
    slice_4 := []byte{'A', 'n', 'M', 'o', 'p', 'Q'}
    //스ライ스 표시
    fmt.Println("스ライ스 1: ", slice_1)
    fmt.Println("스ライ스 2: ", slice_2)
    fmt.Println("스ライ스 3: ", slice_3)
    fmt.Println("스ライ스 4: ", slice_4)
    //스ライ스 비교, Compare 사용
    res1 := bytes.Compare(slice_1, slice_2)
    res2 := bytes.Compare(slice_1, slice_3)
    res3 := bytes.Compare(slice_1, slice_4)
    res4 := bytes.Compare(slice_2, slice_3)
    res5 := bytes.Compare(slice_2, slice_4)
    res6 := bytes.Compare(slice_2, slice_1)
    res7 := bytes.Compare(slice_3, slice_1)
    res8 := bytes.Compare(slice_3, slice_2)
    res9 := bytes.Compare(slice_3, slice_4)
    res10 := bytes.Compare(slice_4, slice_1)
    res11 := bytes.Compare(slice_4, slice_2)
    res12 := bytes.Compare(slice_4, slice_3)
    res13 := bytes.Compare(slice_4, slice_4)
    fmt.Println("\n결과 1:", res1)
    fmt.Println("결과 2:", res2)
    fmt.Println("결과 3:", res3)
    fmt.Println("결과 4:", res4)
    fmt.Println("결과 5:", res5)
    fmt.Println("결과 6:", res6)
    fmt.Println("결과 7:", res7)
    fmt.Println("결과 8:", res8)
    fmt.Println("결과 9:", res9)
    fmt.Println("결과 10:", res10)
    fmt.Println("결과 11:", res11)
    fmt.Println("결과 12:", res12)
    fmt.Println("결과 13:", res13)
}

출력:

스ライ스 1:  [65 78 77 79 80 81]
스ライ스 2:  [97 103 116 101 113 109]
스ライ스 3:  [65 78 77 79 80 81]
스ライ스 4:  [65 110 77 111 112 81]
결과 1: -1
결과 2: 0
결과 3: -1
결과 4: 1
결과 5: 1
결과 6: 1
결과 7: 0
결과 8: -1
결과 9: -1
결과 10: 1
결과 11: -1
결과 12: 1
결과 13: 0