Golang bytes包Buffer源码分析

【Flink】Flink here spill the th sort buffer data with bytes and compressed bytes flink batch sql 好几换个任务有如下打印先说结论:这个是 BinaryExternalSorter是一个完全成熟的二进制格式排序器。它实现了多路归并排序。在内部,它有三个异步线程(排序、溢出、合并),它们通过一组阻塞的循环队列进行通信,形成一个闭环。使用MemoryManager接口分配内存。因此,该组件不会超过所提供的内存限制。所以你这里是排序的时候 内存不足 溢写数据到磁盘了没有什么大问题。可以扩大内存,可能就没有这个日志了。 阅读详情

bytes/buffer.go

Buffer提供了一个可扩展的字节缓冲区,底层是对[]byte进行封装,提供读写的功能。

 

结构体

type Buffer struct {
   buf       []byte   // contents are the bytes buf[off : len(buf)]  缓冲区
   off       int      // read at &buf[off], write at &buf[len(buf)]  读写的索引值,指针偏移量
   bootstrap [64]byte // memory to hold first slice; helps small buffers avoid allocation.  存储第一切片,避免小缓冲区分配
   lastRead  readOp   // last read operation, so that Unread* can work correctly.上次读的操作,用于UnReadRune等撤回到上次读操作之前的状态,所以记录最新一次的操作,自动扩容使用

   // FIXME: it would be advisable to align Buffer to cachelines to avoid false
   // sharing.
}

Buffer是一个可变大小的字节缓冲区,具有Read和Write方法。 Buffer的零值是一个可以使用的空缓冲区。

 

初始化

func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} }

  根据buf初始化buffer缓冲区

 

func NewBufferString(s string) *Buffer {
   return &Buffer{buf: []byte(s)}
}

根据字符串初始化并返回buffer

 

readOp常量描述了对缓冲区执行的最后一个操作

type readOp int8

// Don't use iota for these, as the values need to correspond with the
// names and comments, which is easier to see when being explicit.
const (
   opRead      readOp = -1 // Any other read operation.  任何其他操作
   opInvalid   readOp = 0  // Non-read operation.    没有读操作
   opReadRune1 readOp = 1  // Read rune of size 1.  读取大小为1的字符 (由于UTF-8字符可能包含1-4个字节)
   opReadRune2 readOp = 2  // Read rune of size 2.  读取大小为2的字符
   opReadRune3 readOp = 3  // Read rune of size 3.  读取大小为3的字符
   opReadRune4 readOp = 4  // Read rune of size 4.  读取大小为4的字符
)

 

func (b *Buffer) Bytes() []byte { return b.buf[b.off:] }

作用:用于获取未读部分的buffer数据
Bytes返回一个长度为b.Len()的片段,其中包含缓冲区的未读部分。
切片仅在下一次缓冲区修改之前有效(即,直到下一次调用Read,Write,Reset或Truncate之类的方法)。

 

func (b *Buffer) String() string {
   if b == nil {
      // Special case, useful in debugging.
      return "<nil>"
   }
   return string(b.buf[b.off:])
}

作用:返回缓冲区中未读部分的字符串形式

 

// empty returns whether the unread portion of the buffer is empty.
func (b *Buffer) empty() bool { return len(b.buf) <= b.off }

作用:返回缓冲区的未读部分是否为空

 

//Len returns the number of bytes of the unread portion of the buffer;
func (b *Buffer) Len() int { return len(b.buf) - b.off }

作用:返回缓冲区未读部分的字节数;

 

// Cap returns the capacity of the buffer's underlying byte slice, that is, the
// total space allocated for the buffer's data.
func (b *Buffer) Cap() int { return cap(b.buf) }

作用:返回缓冲区切片的容量,即为缓冲区数据分配的总空间。

 

// Truncate discards all but the first n unread bytes from the buffer
// but continues to use the same allocated storage.
// It panics if n is negative or greater than the length of the buffer.
func (b *Buffer) Truncate(n int) {
   if n == 0 {
      b.Reset()
      return
   }
   b.lastRead = opInvalid
   if n < 0 || n > b.Len() {
      panic("bytes.Buffer: truncation out of range")
   }
   b.buf = b.buf[:b.off+n]
}

作用:从缓冲区中丢弃除前n个未读字节以外的所有字节,但继续使用相同的已分配存储。(已读的数据不会删除)
如果n为负或大于缓冲区的长度,则会发生panic。

 

// Reset resets the buffer to be empty,
// but it retains the underlying storage for use by future writes.
// Reset is the same as Truncate(0).
func (b *Buffer) Reset() {
   b.buf = b.buf[:0]
   b.off = 0
   b.lastRead = opInvalid
}

重置操作

将缓冲区重置为空,但它会保留底层存储以供将来的写入使用。(清空数据,cap不变)
offset 偏移量置为0
lastRead置为未读取

 

// tryGrowByReslice is a inlineable version of grow for the fast-case where the
// internal buffer only needs to be resliced.
// It returns the index where bytes should be written and whether it succeeded.
//适用于内部缓冲区只需要复制的快速情况。
// 它返回开始写入字节的索引以及是否成功。
//判断现有的容量是否需要扩容  ( len(b.off)+n < c )
func (b *Buffer) tryGrowByReslice(n int) (int, bool) {
	if l := len(b.buf); n <= cap(b.buf)-l {
		b.buf = b.buf[:l+n]
		return l, true
	}
   return 0,false
}

 

// grow grows the buffer to guarantee space for n more bytes.
// It returns the index where bytes should be written.
// If the buffer can't grow it will panic with ErrTooLarge.
func (b *Buffer) grow(n int) int {
   m := b.Len()
   // If buffer is empty, reset to recover space.
   if m == 0 && b.off != 0 {
      b.Reset()
   }
   // Try to grow by means of a reslice.
   if i, ok := b.tryGrowByReslice(n); ok {
      return i
   }
   // Check if we can make use of bootstrap array.
   if b.buf == nil && n <= len(b.bootstrap) {
      b.buf = b.bootstrap[:n]
      return 0
   }
   c := cap(b.buf)
   if n <= c/2-m {
      // We can slide things down instead of allocating a new
      // slice. We only need m+n <= c to slide, but
      // we instead let capacity get twice as large so we
      // don't spend all our time copying.
      copy(b.buf, b.buf[b.off:])
   } else if c > maxInt-c-n {
      //panic(ErrTooLarge)
   } else {
      // Not enough space anywhere, we need to allocate.
      buf := makeSlice(2*c + n)
      copy(buf, b.buf[b.off:])
      b.buf = buf
   }
   // Restore b.off and len(b.buf).
   b.off = 0
   b.buf = b.buf[:m+n]
   return m
}

作用:用于缓冲区扩展n个字节的长度。 它返回应写入字节的索引。
扩容规则如下:

1.  len(n.buf) + n <= cap(b.buf)   直接返回 b.buf[:len(b.buf) + n]
2.  b.buf == nil && n <= len(b.bootstrap) 直接返回 b.bootstrap[:n]
3.   n + m <= c/2  (m为未读的长度,c是容量),则进行滑动 copy(b.buf, b.buf[b.off:]) ,将已读的部分移除
4. buf := makeSlice(2*c + n),容量编程2*c + n,即时原来容量的2倍+n

 

// makeSlice allocates a slice of size n. If the allocation fails, it panics
// with ErrTooLarge.
func makeSlice(n int) []byte {
   // If the make fails, give a known error.
   defer func() {
      if recover() != nil {
         panic(ErrTooLarge)
      }
   }()
   return make([]byte, n)
}

分配一个切片大小为n的切片。

 

// Grow grows the buffer's capacity, if necessary, to guarantee space for
// another n bytes. After Grow(n), at least n bytes can be written to the
// buffer without another allocation.
// If n is negative, Grow will panic.
// If the buffer can't grow it will panic with ErrTooLarge.
func (b *Buffer) Grow(n int) {
   if n < 0 {
      panic("bytes.Buffer.Grow: negative count")
   }
   m := b.grow(n)
   b.buf = b.buf[:m]
}

如果需要,Grow会增加缓冲区的容量,以保证另外n个字节的空间。
调用grow()进行扩容

 

// Write appends the contents of p to the buffer, growing the buffer as
// needed. The return value n is the length of p; err is always nil. If the
// buffer becomes too large, Write will panic with ErrTooLarge.
func (b *Buffer) Write(p []byte) (n int, err error) {
   b.lastRead = opInvalid
   //ok是否需要扩容,m是开始写的位置索引
   m, ok := b.tryGrowByReslice(len(p))
   if !ok {
      //扩容,m是开始写的位置
      m = b.grow(len(p))
   }
   return copy(b.buf[m:], p), nil
}

作用:将切片p的内容写到buffer缓冲区

 

// WriteString appends the contents of s to the buffer, growing the buffer as
// needed. The return value n is the length of s; err is always nil. If the
// buffer becomes too large, WriteString will panic with ErrTooLarge.
func (b *Buffer) WriteString(s string) (n int, err error) {
   b.lastRead = opInvalid
   m, ok := b.tryGrowByReslice(len(s))
   if !ok {
      m = b.grow(len(s))
   }
   return copy(b.buf[m:], s), nil
}

作用:将字符串s写入到缓冲区buffer

 

// MinRead is the minimum slice size passed to a Read call by
// Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond
// what is required to hold the contents of r, ReadFrom will not grow the
// underlying buffer.
//MinRead是Buffer.ReadFrom传递给Read调用的最小切片大小。
// 只要Buffer至少具有保存r内容所需的MinRead字节,ReadFrom就不会增长底层缓冲区。
const MinRead = 512


// ReadFrom reads data from r until EOF and appends it to the buffer, growing
// the buffer as needed. The return value n is the number of bytes read. Any
// error except io.EOF encountered during the read is also returned. If the
// buffer becomes too large, ReadFrom will panic with ErrTooLarge.
func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
   b.lastRead = opInvalid
   for {
      i := b.grow(MinRead)
      m, e := r.Read(b.buf[i:cap(b.buf)])
      if m < 0 {
         panic(errNegativeRead)
      }

      b.buf = b.buf[:i+m]
      n += int64(m)
      if e == io.EOF {
         return n, nil // e is EOF, so return nil explicitly
      }
      if e != nil {
         return n, e
      }
   }
}

作用:ReadFrom从r读取数据直到EOF并将其附加到缓冲区,根据需要自动扩容缓冲区。返回值n是读取的字节数。 除了在读取期间遇到的io.EOF之外的任何错误也会被返回。如果缓冲区变得太大,ReadFrom可能会触发ErrTooLarge panic

 

// WriteTo writes data to w until the buffer is drained or an error occurs.
// The return value n is the number of bytes written; it always fits into an
// int, but it is int64 to match the io.WriterTo interface. Any error
// encountered during the write is also returned.
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
   b.lastRead = opInvalid
   if nBytes := b.Len(); nBytes > 0 {
      m, e := w.Write(b.buf[b.off:])
      if m > nBytes {
         panic("bytes.Buffer.WriteTo: invalid Write count")
      }
      b.off += m
      n = int64(m)
      if e != nil {
         return n, e
      }
      // all bytes should have been written, by definition of
      // Write method in io.Writer
      if m != nBytes {
         return n, io.ErrShortWrite
      }
   }
   // Buffer is now empty; reset.
   b.Reset()
   return n, nil
}

WriteTo方法将缓冲区的数据写入w,直到缓冲区耗尽或发生错误。返回值n是写入的字节数; 它总是适合int,但它是int64以匹配io.WriterTo接口。写入期间遇到的任何错误也会返回。

 

// WriteByte appends the byte c to the buffer, growing the buffer as needed.
// The returned error is always nil, but is included to match bufio.Writer's
// WriteByte. If the buffer becomes too large, WriteByte will panic with
// ErrTooLarge.
func (b *Buffer) WriteByte(c byte) error {
   b.lastRead = opInvalid
   m, ok := b.tryGrowByReslice(1)
   if !ok {
      m = b.grow(1)
   }
   b.buf[m] = c
   return nil
}

WriteByte将字节c附加到缓冲区,根据需要自动扩容缓冲区。返回的错误总是为nil,如果缓冲区变得太大超过了ErrTooLarge会发生panic。

 

// WriteRune appends the UTF-8 encoding of Unicode code point r to the
// buffer, returning its length and an error, which is always nil but is
// included to match bufio.Writer's WriteRune. The buffer is grown as needed;
// if it becomes too large, WriteRune will panic with ErrTooLarge.
func (b *Buffer) WriteRune(r rune) (n int, err error) {
   //r为一个字节,则调用WriteByte
   if r < utf8.RuneSelf {
      b.WriteByte(byte(r))
      return 1, nil
   }
   //判断是否需要扩容,如果需要则进行扩容,r的最长字节数可能是utf8.UTFMax(即4个字节)
   b.lastRead = opInvalid
   m, ok := b.tryGrowByReslice(utf8.UTFMax)
   if !ok {
      m = b.grow(utf8.UTFMax)
   }
   //将r写入到缓冲区中
   n = utf8.EncodeRune(b.buf[m:m+utf8.UTFMax], r)
   b.buf = b.buf[:m+n]
   return n, nil
}

WriteRune将Unicode字符r的UTF-8编码附加到缓冲区,返回其长度和错误,
如果缓冲区变得太大超过了ErrTooLarge会发生panic。

 

// Read reads the next len(p) bytes from the buffer or until the buffer
// is drained. The return value n is the number of bytes read. If the
// buffer has no data to return, err is io.EOF (unless len(p) is zero);
// otherwise it is nil.
func (b *Buffer) Read(p []byte) (n int, err error) {
   b.lastRead = opInvalid
   //判断缓冲区内的数据是否为空
   if b.empty() {
      // Buffer is empty, reset to recover space.
      b.Reset()
      if len(p) == 0 {
         return 0, nil
      }
      return 0, io.EOF
   }
   //将缓冲区的数据cope到p
   n = copy(p, b.buf[b.off:])
   b.off += n
   if n > 0 {
      b.lastRead = opRead
   }
   return n, nil
}

作用:从缓冲区读取len(p) 字节到p切片中,如果缓冲区中剩余的元素个数小于len(p)则将缓冲区中剩余的元素读取到p切片中。 返回值n是读取的字节数。如果缓冲区中没有数据,则err为io.EOF(除非len(p)为零); 否则它是nil。

 

// Next returns a slice containing the next n bytes from the buffer,
// advancing the buffer as if the bytes had been returned by Read.
// If there are fewer than n bytes in the buffer, Next returns the entire buffer.
// The slice is only valid until the next call to a read or write method.
func (b *Buffer) Next(n int) []byte {
   b.lastRead = opInvalid
   //获取缓冲区buffer中未读的长度m
   m := b.Len()
   if n > m {
      n = m
   }

   data := b.buf[b.off : b.off+n]
   b.off += n
   if n > 0 {
      b.lastRead = opRead
   }
   return data
}

Next用于获取缓冲区中接下来n个未读字节,并移动缓冲区的指针偏移量off,就好像Read读取数据一样。如果缓冲区中的字节少于n个,则Next返回整个缓冲区。 切片仅在下次调用read或write方法之前有效,Next也属于读操作,因为指针偏移量会增加
 

// ReadByte reads and returns the next byte from the buffer.
// If no byte is available, it returns error io.EOF.
func (b *Buffer) ReadByte() (byte, error) {
   //如果为空,则复位,返回io.EOF
   if b.empty() {
      // Buffer is empty, reset to recover space.
      b.Reset()
      return 0, io.EOF
   }
   //获取接下来一个未读字节,偏移量索引+1,标记上次读操作是opRead操作
   c := b.buf[b.off]
   b.off++
   b.lastRead = opRead
   return c, nil
}

读取缓冲区下一个未读字节,如果该缓冲区没有可读的字节,则复位缓冲区,返回io.EOF

 

// ReadRune reads and returns the next UTF-8-encoded
// Unicode code point from the buffer.
// If no bytes are available, the error returned is io.EOF.
// If the bytes are an erroneous UTF-8 encoding, it
// consumes one byte and returns U+FFFD, 1.
func (b *Buffer) ReadRune() (r rune, size int, err error) {
   //判断是否为空
   if b.empty() {
      // Buffer is empty, reset to recover space.
      b.Reset()
      return 0, 0, io.EOF
   }
   //如果未读的接下来一个字符是ASCII,
   c := b.buf[b.off]
   if c < utf8.RuneSelf {
      b.off++
      b.lastRead = opReadRune1
      return rune(c), 1, nil
   }
   //读取接下来一个utf-8字符,r是该字符,n是字符长度
   r, n := utf8.DecodeRune(b.buf[b.off:])
   b.off += n
   b.lastRead = readOp(n)
   return r, n, nil
}

读取一个utf-8字符,返回f该字符和字符的长度size

// UnreadRune unreads the last rune returned by ReadRune.
// If the most recent read or write operation on the buffer was
// not a successful ReadRune, UnreadRune returns an error.  (In this regard
// it is stricter than UnreadByte, which will unread the last byte
// from any read operation.)
func (b *Buffer) UnreadRune() error {
   //如果上次操作不是ReadRune(),则返回错误
   if b.lastRead <= opInvalid {
      return errors.New("bytes.Buffer: UnreadRune: previous operation was not a successful ReadRune")
   }

   //偏移量off回到ReadRune之前的状态
   if b.off >= int(b.lastRead) {
      b.off -= int(b.lastRead)
   }
   b.lastRead = opInvalid
   return nil
}

UnreadRune方法用于撤回上次ReadRune操作的数据,如果上次操作不是ReadRune(),则返回错误

 

// UnreadByte unreads the last byte returned by the most recent successful
// read operation that read at least one byte. If a write has happened since
// the last read, if the last read returned an error, or if the read read zero
// bytes, UnreadByte returns an error.
func (b *Buffer) UnreadByte() error {
   if b.lastRead == opInvalid {
      return errors.New("bytes.Buffer: UnreadByte: previous operation was not a successful read")
   }
   b.lastRead = opInvalid
   if b.off > 0 {
      b.off--
   }
   return nil
}

UnreadByte方法用于撤销上次的ReadByte数据,如果上次不是ReadByte()则返回错误

 

// ReadBytes reads until the first occurrence of delim in the input,
// returning a slice containing the data up to and including the delimiter.
// If ReadBytes encounters an error before finding a delimiter,
// it returns the data read before the error and the error itself (often io.EOF).
// ReadBytes returns err != nil if and only if the returned data does not end in
// delim.
func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) {
   slice, err := b.readSlice(delim)
   // return a copy of slice. The buffer's backing array may
   // be overwritten by later calls.
   line = append(line, slice...)
   return line, err
}

ReadBytes方法读取直到第一次出现delim字节,如果delim不在剩余的buffer中,则读完

 

// readSlice is like ReadBytes but returns a reference to internal buffer data.
func (b *Buffer) readSlice(delim byte) (line []byte, err error) {
   i := IndexByte(b.buf[b.off:], delim)
   end := b.off + i + 1
   if i < 0 {
      //len(b.buf)是缓冲区中数据的长度
      end = len(b.buf)
      err = io.EOF
   }
   line = b.buf[b.off:end]
   b.off = end
   b.lastRead = opRead
   return line, err
}

读取缓冲区剩余的数据,直到delim字节为止
如果delim字节找不到,将剩余所有的数据读完
和ReadBytes不同的是readSlice直接返回buffer数据的引用

 

// ReadString reads until the first occurrence of delim in the input,
// returning a string containing the data up to and including the delimiter.
// If ReadString encounters an error before finding a delimiter,
// it returns the data read before the error and the error itself (often io.EOF).
// ReadString returns err != nil if and only if the returned data does not end
// in delim.
func (b *Buffer) ReadString(delim byte) (line string, err error) {
   slice, err := b.readSlice(delim)
   return string(slice), err
}

ReadString方法和ReadBytes功能相似,只是返回字符串

AI 代码生成工具的质量评估体系 基础需求层包括响应速度(Response Time)和界面友好性(UI Friendliness),期望需求层涵盖智能提示(Smart Suggestions)和个性化配置(Personalization),兴奋需求层则涉及多模态交互(Multimodal Interaction)和代码进化(Code Evolution)。在金融领域,代码审计合规性(Compliance Audit)权重应提升至40%,而在Web开发中,API兼容性(API Compatibility)的权重需达到35%。 阅读详情

相关推荐

从部署到养成:Hermes智能体接入DeepSeek与微信机器人的完整实战指南

智能体(Agent)正成为大模型从“对话”走向“行动”的关键形态。它通过拆解指令、调用工具、执行任务,让AI真正参与工作流。Hermes作为可本地部署的开源智能体框架,支持接入DeepSeek等主流大模型,并提供网关、工具调用、记忆管理等模块化能力。从Docker容器部署到微信机器人桥接,从自动化定时任务到Computer Use桌面控制,Hermes将个人助理从概念落地为可运行的工程系统。本文基于真实实践,梳理架构原理、部署流程、API配置与高频故障排查,为技术玩家提供一套从零到一的上手指南。

weixin_29045001的博客 264

Golang 中的 bytes 包详解(三):常用函数

Golang 中的 bytes 包提供了许多操作字节切片(Byte slices)的函数和方法,可以简单高效地处理字节数据。之前讲解了 bytes.Reader 和 bytes.Buffer 这两个结构体的使用方法、特性和使用场景,本文将详细介绍 bytes 包提供的常用函数,并给出一些示例代码来帮助大家更好地理解。

路多辛的所思所想 827

使用Python开发TreeATE的测试脚本常见问题

ImportError: No module named py_compile 找不到 DevLangPython3.dll 以上2个问题都是没有安装正确的Python,当前TreeATE依赖32位Python 2.7版本,暂时不支持Python 3以上版本

蓝白云的专栏 1033

Go语言学习笔记—golang标准库bytes包

bytes包提供了对字节切片进行读写操作的一系列函数,字节切片处理的函数比较多分为基本处理函数、比较函数、后缀检查函数、索引函数、分割函数、大小写处理函数和子切片处理函数等。

qq_39280718的博客 2057

利用OrthoFinder做泛基因家族聚类分析

这条命令会将 Arabidopsis_thaliana.TAIR10.59_longest_trans.pep.fa 文件中的所有序列ID,去掉ID中的空格后面的部分,只保留 > 后第一个空格之前的基因ID,并将结果保存在 An1.fasta 文件中。muscle 是一种常用的多序列比对工具,用于基因组或蛋白质序列的比对。基因家族(Gene family),是来源于同一个祖先,由一个基因通过基因重复而产生两个或更多的拷贝而构成的一组基因,它们在结构和功能上具有明显的相似性,编码相似的蛋白质产物。

qq_64400864的博客 2755

Python爬虫实战:逆向解析央视频加密参数(附完整代码)

本文详细解析了央视频加密体系的技术实现,通过Python逆向工程实战演示如何解密动态密钥、AES加密和自定义哈希算法等核心组件。文章提供完整代码示例,涵盖GUID生成、参数构造和异步请求处理,帮助开发者掌握现代视频平台的反爬机制破解方法。

weixin_42532473的博客 1131

Golang strings包

strings包主要是提供string字符串的相关处理函数,主要包括: 比较 //比较a与b,相等返回0,a>b则1,a<b则-1 func Compare(a, b string) int //字符串s和t比较,它们在全部小写的情况下,采用UTF8编码的底层的unicode是否一致 func EqualFold(s, t string) bool 包含 //这个函数主要是用来判断...

Mr.Buffoon 497

Keil5保姆级安装指南:从破解到STM32开发环境配置(附常见错误解决方案)

本文提供Keil5从安装到STM32开发环境配置的完整指南,包含破解步骤、常见错误解决方案及多芯片支持配置。详细讲解环境预检、核心安装、许可证管理及高效开发环境设置,帮助开发者快速搭建嵌入式开发平台。特别针对安装和卸载过程中的关键问题提供专业解决方案。

weixin_30369041的博客 126

golang中bytes包用法

本文转自 Golove的博客http://www.cnblogs.com/golove/p/3287729.html bytes 包中的函数和方法 // bytes 包实现了用于操作 []byte 的函数,类似于 strings 包中的函数 // bytes.go -----------------------------------------------...

weixin_33968104的博客 384

某东x-api-eid-token风控参数分析与破解

x-api-eid-token是京东的风控参数,在接口当中,如果没有该参数,有一定的概率能请求成功,但是在请求一段时间后,会报403权限失败的,该参数主要通过jsTk.do这个接口生成,并且会将其存储到cookie当中。

zhouzhou_98的博客 1552

【LAMMPS学习】五、LAMMPS命令(8) Pair_style 势

所有LAMMPS pair_style命令如下表所示。有些样式有加速版本。这由括号中的附加字母表示:g = GPU, i = INTEL, k = KOKKOS, o = OPENMP, t = OPT。

荷塘阅色 3016

Golang 中的 bytes 包详解(二):bytes.Reader

Golang 中的 bytes 包是其中一个 IO 操作标准库,实现了对字节切片([]byte)的操作,提供了类似于 strings 包的功能。本文先讲解一下 bytes 包中的结构体 bytes.Reader。

路多辛的所思所想 1946

Electron vue3 - 使用SerialPort与串口交互教程,计算机串行端口(COM口、USB转串口等)进行通信连接读取或写入Arduino、单片机、PLC、扫码枪、打印机、老式调制解调器!

实战Electron使用SerialPort与串口交互详细全过程,Electron-vue3与串口交互示例,Electron vue使用serialport实现串口交互通讯,Electron-vue使用serialport连接本地串口,electron-vue 台称串口对接 SerialPort,electron串口调试工具,electron+vue+serialport实现串口通信,串口通信serialport使用和调试,electron-vue使用serialport串口通信及踩过的坑(已解决!)ele

🏆 前端领域优质创作者 4642

Golang bytes.Buffer源码分析

Golang bytes.Buffer分析 Buffer作为高效缓存支持读、写与撤销读取操作,可用于字节流缓存与解析等。这里简要分析一下其源码实现。 本文源码版本为go1.16.3 linux/amd64 自增长规则 Buffer可以在长度不足时增加自身长度,其核心增长函数是grow(n int) int私有函数,源码在go/src/bytes/buffer.go第117行: // grow grows the buffer to guarantee space for n more bytes. //

QixuanAIRobot的专栏 664

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

golang 之bytes.Buffer 写操作扩容解读

golang(go1.12.9) 的标准包bytes.Buffer,是一个简单的字节(byte) 缓冲buffer, 定义了一个默认的最小容量smallBufferSize = 64,实现了byte的读,写,清空等操作, 其中写操作稍微麻烦,需要考虑扩容的情形,下面主要分析一下buffer 扩容 首先,看一下buffer结构体的定义 type Buffer struct { buf [...

西格玛的博客 2731

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Golang源码分析bytes.go

Golang源码分析bytes.go

gopher123的博客 1128

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Go 语言 bytes.Buffer 源码详解 2

Go 语言中的 bytes.Buffer 是大小可变的字节缓冲区,本篇文章,我们对 Buffer 结构源码进行了逐行分析,了解了底层读写方法的实现原理,并简单介绍了使用方法。

u012948302的专栏 733

一文学会Golang里拼接字符串的6种方式(性能对比)

本文详细比较了Go语言中不同字符串拼接方式,包括基本运算符+、fmt.Sprintf、strings.Join、strings.Builder、bytes.Buffer以及使用[]byte的性能。实验结果显示strings.Builder和bytes.Buffer在效率上有显著优势。

LightOfNight的博客 2607

grpc-go源码剖析四十六之服务器端是如何将数据帧缓存到recvBuffer里?(多个数据帧时,在底层是如何来存储的?)

经过前面几篇文章分析,我们已经服务器端接收到数据帧后,经历抽样流控,链路流控,流级别流控; 那么,接下来我们要考虑的问题是,服务器端如何来维护接收到数据帧呢? 换句话说,服务器端接收到不止一个数据帧时,按照什么原则来存储这些数据帧? 是随意存储,还是存储到单链表里呢? 1、数据帧存储到recvBuffer里的整体流程图? 1.1、存储模型图? 数据帧在grpc框架中的存储是利用channel+bytes.Buffer作为底层存储的(recvBuffer.c + recvBuffer.backlog)。

码二哥的技术池 598

go字符串拼接方式及性能比拼

在golang中字符串的拼接方式有多种,本文将会介绍比较常用的几种方式,并且对各种方式进行压测,以此来得到在不同场景下更适合使用的方案。

Peerless__的博客 3167

Go语言6种字符串拼接的方式

Go语言6种字符串拼接的方式前言string类型字符串拼接的6种方式及原理原生拼接方式"+"字符串格式化函数fmt.SprintfStrings.builderbytes.Bufferstrings.join切片appendBenchmark对比总结 前言 本文使用Go语言版本:1.17.1 string类型 我们首先来了解一下Go语言中string类型的结构定义,先来看一下官方定义: // string is the set of all strings of 8-bit bytes, conventi

Salted fish in hand, I have the world. 5508

go字符串拼接操作

1. 使用 + 拼接 通过查看汇编代码可知 + 实际上调用的是 runtime/string.go中的concatstrings 函数,该函数源代码如下: // concatstrings implements a Go string concatenation x+y+z+... // The operands are passed in the slice a. // If buf !...

冬木 3555

用数据说话,Go 所有字符串拼接方式里哪种才是最稳定高效的?

前言 日常业务开发中离不开字符串的拼接操作,不同语言的字符串实现方式都不同,在Go语言中就提供了6种方式进行字符串拼接,那这几种拼接方式该如何选择呢?使用那个更高效呢?今天我们邀请到公众号「Golang 梦工厂」的主理人松哥就一起来分析一下。 本文使用Go语言版本:1.17.1 string类型 我们首先来了解一下Go语言中string类型的结构定义,先来看一下官方定义: //stringisthesetofallstringsof8-bitbytes,conventi...

彪锅锅来啦 427

十二. go 高性能编程之 sync.Pool的复用对象

【代码】十二. go 高性能编程之 sync.Pool的复用对象。

qq_29799655的博客 732

【String拼接】Go语言字符串如何高效的进行拼接(6种方式进行对比分析)

string类型 我们首先来了解一下Go语言中string类型的结构定义,先来看一下官方定义: // string is the set of all strings of 8-bit bytes, conventionally but not // necessarily representing UTF-8-encoded text. A string may be empty, but // not nil. Values of string type are immutable. type str.

码农印象 2936

http2curl性能优化终极指南:深入源码分析转换效率与内存使用技巧

你是否在Golang开发中需要将HTTP请求转换为CURL命令进行调试或测试?http2curl库正是解决这一需求的利器!这个强大的Golang库能够将Go的http.Request对象高效地转换为可执行的CURL命令行,极大简化了API调试和请求复现的过程。今天,我们将从源码角度深入分析http2curl的性能优化策略,帮助你掌握提升转换效率与优化内存使用的核心技巧。 ## 📊 http2c

gitblog_00066的博客 837
上一篇: Golang bytes源码分析
下一篇: Golang context包源码分析
思维的深度
博客等级 码龄11年 2万+粉丝 391原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值