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

Golang 기본 강의

Golang 제어 문

Golang 함수 & 메서드

Golang 구조체

Golang 슬라이스 & 배열

Golang 문자열(String)

Golang 포인터

Golang 인터페이스

Golang 병행

Golang 예외(Error)

Golang 다른Miscellaneous

Go 언어 슬라이스 분할

Go 바이트 슬라이스에서는, 사용할 수 있습니다Split()주어진 슬라이스를 분할하는 함수입니다. 이 함수는 주어진 구분자로 바이트 슬라이스를 모든 서브 슬라이스로 분할하고, 이러한 모든 서브 슬라이스를 포함한 슬라이스를 반환합니다. 이 함수는 bytes 패키지에서 정의되므로, Split 함수에 액세스하기 위해 bytes 패키지를 프로그램에 가져오는必要가 있습니다.

문법:

func Split(o_slice, sep []byte) [][]byte

여기서는o_slice은 바이트 슬라이스입니다sep은 구분자입니다. 만약sep비어 있으면, 각 UTF-8시퀀스 이후 분할. 이 개념에 대해 예제를 통해 논의해 보겠습니다:

바이트 슬라이스 분할 예제:

//바이트 슬라이스 분할 방법
package main
import (
    "bytes"
    "fmt"
)
func main() {
    //생성 및 초기화
    //바이트 스리스
    //사용 간축 표기법
    slice_1 [:= []byte{'!', '!', 'G', 'e', 'e', 'k', 's',
        'f', 'o', 'r', 'G', 'e', 'e', 'k', 's', '#', '#'}
    slice_2 := []byte{'A', 'p', 'p', 'l', 'e'}
    slice_3 := []byte{'%', 'g', '%', 'e', '%', 'e',
        '%', 'k', '%', 's', '%'}
    //스리스 표시
    fmt.Println("원본 스리스:")
    fmt.Printf("Slice 1: %s", slice_1)
    fmt.Printf("\nSlice 2: %s", slice_2)
    fmt.Printf("\nSlice 3: %s", slice_3)
    //바이트 스리스 분할
    //분할 함수 사용
    res1 := bytes.Split(slice_1, []byte("eek"))
    res2 := bytes.Split(slice_2, []byte(""))
    res3 := bytes.Split(slice_3, []byte("%"))
    //결과 표시
    fmt.Printf("\n\n분할 후:")
    fmt.Printf("\nSlice 1: %s", res1)
    fmt.Printf("\nSlice 2: %s", res2)
    fmt.Printf("\nSlice 3: %s", res3)
}

출력:

원본 스리스:
슬라이스 1: !!GeeksforGeeks##
슬라이스 2: A p p l e
슬라이스 3: %g%e%e%k%s%
분할 후:
슬라이스 1: [!!G sforG s##]
슬라이스 2: [A p p l e]
슬라이스 3: [ g e e k s]

바이트 스리스 분할 메서드 예제2:

//바이트 스리스 분할 메서드
package main
import (
    "bytes"
    "fmt"
)
func main() {
    //생성 및 분할
    //바이트 스리스
    //분할 함수 사용
    res1 := bytes.Split([]byte("****Welcome, to, w3codebox****)), []byte(","))
    res2 := bytes.Split([]byte("Learning x how x to x trim x a x slice x of x bytes"), []byte("x"))
    res3 := bytes.Split([]byte("w3codebox, Geek"), []byte(""))
    res4 := bytes.Split([]byte(""), []byte(","))
    //결과 표시
    fmt.Printf("최종 결과 값:\n")
    fmt.Printf("\nSlice 1: %s", res1)
    fmt.Printf("\nSlice 2: %s", res2)
    fmt.Printf("\nSlice 3: %s", res3)
    fmt.Printf("\nSlice 4: %s", res4)
}

출력:

최종 결과 값:
슬라이스 1: [****Welcome  to  w3codebox****]
슬라이스 2: [Learning how to trim a slice of bytes]
슬라이스 3: [n h o o o, G e e k]
슬라이스 4: []