Golang 표준 라이브러리 - bytes(제1절)

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 패키지 읽기 버퍼에 대한 지식을 소개합니다
  • 좋은 웹페이지 즐겨찾기