Use of functions commonly used in bytes:
Package Main;import ("bytes" "FMT" "Unicode")//bytes packages implement a number of functions for []byte operations and two primary reader and buffer two structures func main () {str: = " ABcD ";//converted to lowercase fmt. Println (String (bytes. ToLower ([]byte (str))));//Convert to uppercase FMT. Println (String (bytes. ToUpper ([]byte (str))));//Convert to title, FMT. Println (String (bytes. Totitle ([]byte (str))));//Custom mapping table Mycase: = Unicode. Specialcase{unicode. caserange{//1,1 indicates that the substitution rule affects only character 1,1,[unicode between 1 and 1. maxcase]rune{//Capital Conversion ' one '-1,//lowercase conversion ' A '-1,//title conversion ' Small '-1,},},unicode. Caserange{2,2,[unicode. maxcase]rune{' II '-2, ' two '-2, ' Middle '-2,},},unicode. Caserange{3,3,[unicode. maxcase]rune{' Triple '-3, ' three '-3, ' big '-3,},},};//use the map table to modify []byte characters to lowercase data: = bytes. Tolowerspecial (Mycase,[]byte{1, 2, 3},) fmt. PRINTLN (string data);//Use a mapping table to modify []byte characters to uppercase data = bytes. Toupperspecial (Mycase,[]byte{1, 2, 3},); Fmt. PRINTLN (string data);//Use a mapping table to modify []byte characters to header data = bytes. Totitlespecial (Mycase,[]byte{1, 2, 3},); Fmt. PRINTLN (string data);//Modify the first character in []byte] to title and return fmt.println (string (bytes. Title ([]byte ("ABC def")));//comparison of two []byte,//a < b returns -1//A = = bReturns 0//B > B returns 1FMT. Println (Bytes.compare ([]byte ("a"), []byte ("B")));//comparison of two []byte is equal to FMT. Println (bytes. Equal ([]byte ("abc"), []byte ("abc")));//Compare two []byte are equal, ignore uppercase, lowercase, title fmt. Println (bytes. Equalfold ([]byte ("abc"), []byte ("abc")));//Remove []byte] The character fmt that is contained in Cutset. Println (String (bytes. Trim ([]byte ("abc"), "")));//Remove the character fmt that is contained in Cutset on the left. Println (String (bytes. Trimleft ([]byte ("abc"), "")));//Remove the character fmt that is contained in Cutset on the right. Println (String (bytes. TrimRight ([]byte ("abc"), "")));//Remove both whitespace characters from the FMT. Println (String (bytes. Trimspace ([]byte ("abc")));//Remove prefix FMT. Println (String (bytes. Trimprefix ([]byte ("Tb_user"), []byte ("Tb_")));//Remove suffix FMT. Println (String (bytes. Trimsuffix ([]byte ("User_idx"), []byte ("_idx"))), or//with Sep as a delimiter, cut into multiple []bytetmp: = bytes. Split ([]byte ("AB CD EF"), []byte ("") "); for _, V: = range tmp {FMT. Println (String (v));} Dividing up to n sub-slices, the part that exceeds n will not be sliced tmp = bytes. SPLITN ([]byte ("AB CD EF"), []byte (""), 2); for _, V: = range tmp {FMT. Println (String (v));} With Sep as the delimiter, cut into multiple []byte, the result contains a delimiter, at the tail of the substring tmp = bytes. Splitafter ([]byte ("Ab,cd,ef "), []byte (", ")); for _, V: = range tmp {FMT. Println (String (v));} Dividing up to n sub-slices, the part that exceeds n will not be sliced tmp = bytes. Splitaftern ([]byte ("Ab,cd,ef"), []byte (","), 2); for _, V: = range tmp {FMT. Println (String (v));} Segmentation with blank characters tmp = bytes. Fields ([]byte ("A-B C D")); for _, V: = range tmp {FMT. Println (String (v));} The TMP = bytes is sliced with a character that conforms to the function as a delimiter. Fieldsfunc ([]byte ("ASBSCSD"), func (R rune) bool {if r = = Rune (' s ') {return true;} return false;}); For _, V: = range tmp {FMT. Println (String (v));} Use Sep as the connector, stitching [][]BYTEFMT. Println (String (bytes. Join ([][]byte{[]byte ("AA"), []byte ("BB"), []byte ("CC"),},[]byte ("-"),)));//Repeat []byte,count times fmt.println (String ( bytes. Repeat ([]byte ("abc"), 3));//Determine if there is a prefix fmt. Println (bytes. Hasprefix ([]byte ("Is_true"), []byte ("Is_")));//Determine if there is a suffix fmt. Println (bytes. Hassuffix ([]byte ("chk_on"), []byte ("_on")));//Determine if a []BYTEFMT] is included. Println (bytes. Contains ([]byte ("I Am Jack"), []byte ("Jack"));//Determine if a runefmt is included. Println (bytes. Containsrune ([]byte ("I from China"), Rune (' Medium ')));//Find the location where Sep first appears in parameter one and cannot find the return -1FMT. Println (bYtes. Index ([]byte ("Abcabc"), []byte ("a")), FMT. Println (bytes. Indexbyte ([]byte ("CBA"), ' a '), FMT. Println (bytes. Indexrune ([]byte ("I from China"), Rune (' Medium ')));//Find the location of any character in the chars that appears in parameter one, and cannot find the return -1FMT. Println (bytes. Indexany ([]byte ("Hello World"), "XY"));//function as above, except to find the last occurrence of the FMT. Println (bytes. LastIndex ([]byte ("Abcabc"), []byte ("a")), FMT. Println (bytes. Lastindexbyte ([]byte ("CBA"), ' a '), FMT. Println (bytes. Lastindexany ([]byte ("Hello World"), "XY"));//Gets the number of occurrences of Sep in Parameter one of FMT. Println (bytes. Count ([]byte ("A|b|c"), []byte ("|")); /Replace the first n old in parameter one with new,n less than 0. Fmt. Println (String (bytes. Replace ([]byte ("I Am Jack"), []byte ("I Am"), []byte ("I Am"), "-1,")));//The characters in the []byte are replaced with the return value of the function, and if the return value is negative, the visit character is discarded. Fmt. Println (String (bytes. Map (func (R rune) Rune {if r = = ' A ' {return ' a ';} else if r = = ' C ' {return-1;} Return R;},[]byte ("ABCD"),));//convert []byte to []runefmt]. Println (String (bytes. Runes ([]byte ("Who I Am")));
Use of reader and buffer in bytes:
Package Main;import ("bytes" "FMT")//bytes the use of the reader and buffer two structures in Func Usereader () {data: = "Abcdefghijk";//Through [] BYTE creates Readerre: = bytes. Newreader ([]byte (data));//Returns the length of the unread portion of FMT. Println ("Re len:", re.) Len ());//Returns the total length of the underlying data fmt. Println ("Re Size:", re.) Size ()); BUF: = Make ([]byte, 2); for {//Read data n, err: = Re. Read (BUF); if err! = nil {break;} Fmt. Println (String (buf[:n));};/ /Set the offset, because the above operation has modified the information such as read position re. Seek (0, 0); for {//a byte read B, err: = Re. ReadByte (); if err! = nil {break;} Fmt. Println (string (b));} Re. Seek (0, 0); Off: = Int64 (0); for {//Specify offset read N, err: = Re. ReadAt (buf, off); if err! = nil {break;} Off + = Int64 (n); fmt. Println (Off, String (Buf[:n]));}} Func Usebuffer () {data: = "123456789";//Create a BUFFERBF by []byte: = bytes. Newbuffer ([]byte (data));//len () returns the unread data length FMT. Println ("Bf len:", BF.) Len ());//cap () cache capacity FMT. Println ("BF cap:", BF. Cap ());//bytes () returns the unread data slice bys: = bf. Bytes (); for _, V: = range bys {fmt. Print (String (v) + "");} Fmt. Println ();//next () returns a slice of the first n bytes of data that is not read for I: = 0; I < 10; i++ {tmp: = bf. Next (1); Fmt. Print (String (TMP) + "");} Fmt. Println ();//again Next, return []byte, stating that there is no non-read FMT. PRINTLN (BF. Next (1));//reset buffer, discard all contents BF. Reset ();//Create BUFFERBF2 with string: = bytes. newbufferstring (data);//Read the first Delim and its previous contents, return the error encountered line, _: = Bf2. Readbytes (' 3 '); fmt. Println (line);//Effect Ibid., Return stringline2, _: = Bf2. ReadString (' 7 '); Fmt. Println (line2);//Create an empty Bufferbf3: = bytes. buffer{};//automatically increases cache capacity, guaranteeing n bytes of remaining space bf3. Grow (16);//Writes Rune encoding, returns the number of bytes written and the error. N, _: = Bf3. Writerune (Rune (' Medium ')), FMT. Println ("Bf3 write", n); n, _ = bf3. WriteString ("countrymen"); Fmt. Println ("Bf3 write", n);//Returns the non-read string fmt. Println (BF3. String ());//truncates the data length to N-byte bf3. Truncate (6); fmt. Println (BF3. String ()); Func Main () {//prevents too much code in main, I create a new two function to write Usereader () separately; Usebuffer ();}
Common functions of bytes package in go language, reader and buffer usage