golang bytes包解读

golang中的bytes标准库实现了对字节数组的各种操作,与strings标准库功能基本类似。

功能列表:
1、字节切片 处理函数
(1)、基本处理函数
(2)、字节切片比较函数
(3)、前后缀检查函数
(4)、字节切片位置索引函数
(5)、分割函数
(6)、大小写处理函数
(7)、子字节切片处理函数
2、Buffer 对象
3、Reader 对象

基本处理函数
Contains() :返回是否包含子切片
func Contains(b, subslice []byte) bool

案例:
执行结果:
[email protected]  ~/Documents/project/src/test  go build strconv.go
[email protected]  ~/Documents/project/src/test  ./strconv
false
true
true
false

Count():计算子字节切片在字节切片中的重叠数量
func Count(s, sep []byte) int

案例:

执行结果:
[email protected]  ~/Documents/project/src/test  go build strconv.go
[email protected]  ~/Documents/project/src/test  ./strconv
1
2
2
1

Map():首先将字节切片转化为 UTF-8编码的字符序列,然后使用 mapping 将每个Unicode字符映射为对应的字符,最后将结果保存在一个新的字节切片中。
func Map(mapping func(r rune) rune, s []byte) []byte

案例:

执行结果:
[email protected]  ~/Documents/project/src/test  ./strconv
你好,我是杨国强!
你好,我叫杨国强!

原文地址:http://blog.51cto.com/jiekeyang/2178838

时间: 2024-08-11 09:51:51

golang bytes包解读的相关文章

golang bytes 包 详解

概况: 包字节实现了操作字节切片的函数.它类似于琴弦包的设施. 函数: func Compare(a, b []byte) int func Contains(b, subslice []byte) bool func ContainsAny(b []byte, chars string) bool func ContainsRune(b []byte, r rune) bool func Count(s, sep []byte) int func Equal(a, b []byte) bool

GoLang之buffer与bytes包

strings包 strings包的使用举例: package main import s "strings" import "fmt" var p = fmt.Println func main() { p("Contains: ", s.Contains("test", "es")) p("Count: ", s.Count("test", "t&quo

简析 Golang IO 包

简析 Golang IO 包 io 包提供了 I/O 原语(primitives)的基本接口.io 包中定义了四个最基本接口 Reader.Writer.Closer.Seeker 用于表示二进制流的读.写.关闭和寻址操作.这些原语和接口是对底层操作的封装,因此如没有特殊说明,这些原语和接口都不能被视为线程安全的. Reader Reader 接口封装了基本的 Read 方法.Read 读取长度为 len(p) 字节的数据,并写入到 p.返回结果包含读取数据字节数(0 <= n <= len(

Go语言学习(十)bytes包处理字节切片

bytes包提供了对字节切片进行读写操作的一系列函数 字节切片处理的函数比較多,分为基本处理函数,比較函数,后缀检查函数,索引函数,切割函数, 大写和小写处理函数和子切片处理函数等. 1.字节切片基本处理函数api 1.1Contains()函数 //Contains()函数的功能是检查字节切片b是否包括子切片subslice,假设包括返回true,否则返回false. func Contains(b,subslice []bytes) bool 1.2Count()函数 //Count()函数

go语言中bytes包的常用函数,Reader和Buffer的使用

bytes中常用函数的使用: package main; import ( "bytes" "fmt" "unicode" ) //bytes包中实现了大量对[]byte操作的函数和两个最主要的Reader和Buffer两个结构 func main() { str := "aBcD"; //转为小写 fmt.Println(string(bytes.ToLower([]byte(str)))); //转为大写 fmt.Prin

Golang fmt包使用小技巧

h1 { margin-top: 0.6cm; margin-bottom: 0.58cm; direction: ltr; color: #000000; line-height: 200%; text-align: justify; page-break-inside: avoid; orphans: 0; widows: 0 } h1.western { font-family: "Times New Roman", serif; font-size: 22pt } h1.cjk

3.Golang的包导入

1.golang的源码文件可以随意命名,但是属于同一个包的源文件必须声明 package base 2.golang的包引入规则 import ( "fmt" #系统包直接写名字 "github.com/user/stringutil" #其余包 写 src 下的绝对路径 ) go help importpath A few common code hosting sites have special syntax: Bitbucket (Git, Mercuria

Golang Context 包详解

Golang Context 包详解 0. 引言 在 Go 语言编写的服务器程序中,服务器通常要为每个 HTTP 请求创建一个 goroutine 以并发地处理业务.同时,这个 goroutine 也可能会创建更多的 goroutine 来访问数据库或者 RPC 服务. 当这个请求超时或者被终止的时候,需要优雅地退出所有衍生的 goroutine,并释放资源.因此,我们需要一种机制来通知衍生 goroutine 请求已被取消. 比如以下例子,sleepRandom_1 的结束就无法通知到 sle

golang的包管理系统

下面是测试结果 - 自带的vendor - 项目创建vendor子目录,然后github的地址放进去就ok了 - govendor - 安装:go get -u github.com/kardianos/govendor - 初始化:govendor init - 测试:govendor fetch github.com/ziyouchutuwenwu/objective-go - 问题: fetch貌似没有把所有的子目录都弄下来 get装到GOPATH里面去了,蛋疼 - gvt - 安装:go