Golang 표준 라이브러리 - bytes(제1절)
7800 단어 goGolang 표준 라이브러리
bytes go byte , byte , , , , , , , , ;
buffer 。 bytes , , go 。
다음은 방법 소개 뒤에 코드 실례에 따라 설명을 할 것이며 코드는 직접 붙여서 실행할 수 있고 독자가 스스로 테스트할 수 있다.
// func Compare(a, b []byte) int
// a,b , a=b 0,ab 1
fmt.Println(bytes.Compare([]byte{},[]byte{})) // 0
fmt.Println(bytes.Compare([]byte{1},[]byte{2})) // -1
fmt.Println(bytes.Compare([]byte{2},[]byte{1})) // 1
fmt.Println(bytes.Compare([]byte{},nil)) // 0
// func Equal(a, b []byte) bool
//
fmt.Println(bytes.Equal([]byte{},[]byte{})) // true
fmt.Println(bytes.Equal([]byte{'A'},[]byte{'a'})) // false
fmt.Println(bytes.Equal([]byte{},nil)) // true
// func HasPrefix(s, prefix []byte) bool
// s prefix
fmt.Println(bytes.HasPrefix([]byte{1,2,3},[]byte{1})) // true
fmt.Println(bytes.HasPrefix([]byte{1,2,3},[]byte{2})) // false
// func HasSuffix(s, suffix []byte) bool
// s suffix, HasPrefix
// func Contains(b, subslice []byte) bool
// b subslice
fmt.Println(bytes.Contains([]byte{1,2,3},[]byte{1})) // true
fmt.Println(bytes.Contains([]byte{1,2,3},[]byte{1,3})) // false
// func Index(s, sep []byte) int
// sep s , -1
fmt.Println(bytes.Index([]byte{1,2,3,4,5},[]byte{2,3})) // 1
fmt.Println(bytes.Index([]byte{1,2,3,4,5},[]byte{0,1})) // -1
// func IndexByte(s []byte, c byte) int
// c s , -1
fmt.Println(bytes.IndexByte([]byte{1,2,3},2)) // 1
fmt.Println(bytes.IndexByte([]byte{1,2,3},0)) // -1
// func IndexFunc(s []byte, f func(r rune) bool) int
// s f , -1
fmt.Println(bytes.IndexFunc([]byte("hi go"), func(r rune) bool {
return r == 'g'
})) // 3 ; hi go g 3
// func LastIndexFunc(s []byte, f func(r rune) bool) int
// IndexFunc , f ,
// func LastIndex(s, sep []byte) int
// sep s , -1
fmt.Println(bytes.LastIndex([]byte("hi go"),[]byte("go"))) // 3
fmt.Println(bytes.LastIndex([]byte{1,2,3},[]byte{2,3})) // 1
// func ToLower(s []byte) []byte
// s ( )
fmt.Println(string(bytes.ToLower([]byte("ABC")))) // abc
fmt.Println(string(bytes.ToLower([]byte("abc")))) // abc
// func ToUpper(s []byte) []byte
// s , ToLower ,
// func Repeat(b []byte, count int) []byte
// count b
fmt.Println(bytes.Repeat([]byte{1,2},3)) // [1 2 1 2 1 2]
// func Replace(s, old, new []byte, n int) []byte
// s n old new , , n -1 ,
fmt.Println(bytes.Replace([]byte{1,1,3,1,1,3,1,1,4},[]byte{1,1},[]byte{0,0},-1)) // [0 0 3 0 0 3 0 0 4]
// func ReplaceAll(s, old, new []byte) []byte
// ReplaceAll , Replace n -1
// func Map(mapping func(r rune) rune, s []byte) []byte
// s mapping ,
fmt.Println(string(bytes.Map(func(r rune) rune {
return r + 1 // +1
},[]byte("abc")))) // bcd
// func Trim(s []byte, cutset string) []byte
// s cutset
fmt.Println(string(bytes.Trim([]byte("hi go hi js"),"hi"))) // go hi js ; hi, hi
// func TrimSpace(s []byte) []byte
// ,
fmt.Println(bytes.TrimSpace([]byte(" hi go "))) // [104 105 32 103 111]; , ,
// func TrimLeft(s []byte, cutset string) []byte
// s cutset
fmt.Println(string(bytes.TrimLeft([]byte("hihi go"), "hi"))) // go
// func TrimRight(s []byte, cutset string) []byte
// s cutset , TrimLeft
// func TrimPrefix(s, prefix []byte) []byte
// s prefix
fmt.Println(string(bytes.TrimPrefix([]byte("hi go"),[]byte("hi")))) // go
// s suffix ,
// func TrimSuffix(s, suffix []byte) []byte
fmt.Println(string(bytes.TrimSuffix([]byte("hi go"),[]byte("go")))) // hi
// func Fields(s []byte) [][]byte
// s
s := bytes.Fields([]byte(" hi go, hi js, hi c"))
for _,v := range s {
fmt.Println(string(v))// hi|go,|hi|js,|hi|c
}
// func FieldsFunc(s []byte, f func(rune) bool) [][]byte
// Fields , FieldsFunc f
s = bytes.FieldsFunc([]byte(" hi go, hi.js-hi c"),func(r rune) bool {
return r == ','||r == '-'||r == '.' // ,-.
})
for _,v := range s {
fmt.Println(string(v))// hi go| hi|js,|hi c
}
// func Split(s, sep []byte) [][]byte
// sep
s = bytes.Split([]byte(" hihi go hi js hihi c"),[]byte("hi"))
for _,v := range s {
fmt.Println(string(v))// | | go| js| | c
}
// func SplitN(s, sep []byte, n int) [][]byte
// sep ,n ;
// n > 0 : n ; 。
// n == 0: nil
// n < 0 :
// func Join(s [][]byte, sep []byte) []byte
// [][]byte , sep
fmt.Println(bytes.Join([][]byte{{1,1},{2,2},{3,3}},[]byte{0})) // [1 1 0 2 2 0 3 3]
// func EqualFold(s, t []byte) bool
// utf-8 ( unicode 、 、 ) 。 Equal ,
fmt.Println(bytes.EqualFold([]byte{},[]byte{})) // true
fmt.Println(bytes.EqualFold([]byte{'A'},[]byte{'a'})) // true ; , Equal
fmt.Println(bytes.EqualFold([]byte{},nil)) // true
// func Runes(s []byte) []rune
// s []rune 。( utf-8 unicode rune)
// rune
// rune
fmt.Println([]byte("I ")) // [73 32 228 189 160]
fmt.Println(bytes.Runes([]byte("I "))) // [73 32 20320]
//
fmt.Println(len([]byte("I "))) // 5, 5 ‘ ‘ utf8 3 , go utf8
fmt.Println(len(bytes.Runes([]byte("I ")))) // 3 , 3 , utf8 rune
// func Count(s, sep []byte) int
// s sep
fmt.Println(bytes.Count([]byte{1,2,3,5,1,2,5,4},[]byte{1,2})) // 2 , s 1,2 2
fmt.Println(bytes.Count([]byte{1,2,3,5,1,2,5,4},[]byte{1,1})) // 0
// func IndexRune(s []byte, r rune) int
// unicode r utf-8 s , -1
fmt.Println(bytes.IndexRune([]byte("hi "),rune(' '))) // 3
fmt.Println(bytes.IndexRune([]byte("hi "),rune('h'))) // 0
// func IndexAny(s []byte, chars string) int
// chars utf-8 s , chars -1
fmt.Println(bytes.IndexAny([]byte("hello"),"e")) // 1
fmt.Println(bytes.IndexAny([]byte("hello"),"ea")) // 1
fmt.Println(bytes.IndexAny([]byte("hello"),"ae")) // 1
fmt.Println(bytes.IndexAny([]byte("hello"),"c")) // -1
// func LastIndexAny(s []byte, chars string) int
// IndexAny , chars ,
// func Title(s []byte) []byte
// s ( )
fmt.Println(string(bytes.Title([]byte("AAA")))) // AAA
fmt.Println(string(bytes.Title([]byte("aaa")))) // Aaa
// func ToTitle(s []byte) []byte
// ToUpper ,
fmt.Println(string(bytes.ToTitle([]byte("Aaa")))) // AAA
fmt.Println(string(bytes.ToUpper([]byte("Aaa")))) // AAA
// func TrimFunc(s []byte, f func(r rune) bool) []byte
// s f
fmt.Println(string(bytes.TrimFunc([]byte("hhi go"),func(r rune) bool {
return r == 'h' || r == 'o'
}))) // i g;
// func TrimLeftFunc(s []byte, f func(r rune) bool) []byte
// s f , TrimFunc ,
// func TrimRightFunc(s []byte, f func(r rune) bool) []byte
// s f , TrimFunc ,
까지 bytes백의 95% 공구 방법을 소개했다.다음 절에서는 bytes 패키지 읽기 버퍼에 대한 지식을 소개합니다이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Go Fiber 및 PlanetScale로 REST API 구축 - 4부다시 사용자 핸들러에 UpdateUser라는 새 함수를 추가합니다. 업데이트 사용자를 main.go에 등록 이제 응용 프로그램을 다시 실행하십시오. 이전에 생성한 사용자를 업데이트합니다. 응답 사용자가 존재하지 않을...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.