Skip to content

bytes, strings: add Clone #45038

Closed
Closed
@icholy

Description

@icholy

Working directly with []byte is very common and needing to copy it comes up fairly often. A Clone helper would be nice to have.

What did you expect to see?

dup := bytes.Clone(data)

What did you see instead?

dup := make([]byte, len(data))
copy(dup, data)

Implementation

package bytes

// Clone returns a copy of b
func Clone(b []byte) []byte {
  b2 := make([]byte, len(b))
  copy(b2, b)
  return b2
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions