· Zen HuiFer · Learn  · 2 min read

The noCopy strategy you should know in Golang

Discover the importance of noCopy in Golang development. Learn how it prevents accidental copying of critical structures like sync primitives. Enhance your Go code safety and efficiency.

Discover the importance of noCopy in Golang development. Learn how it prevents accidental copying of critical structures like sync primitives. Enhance your Go code safety and efficiency.

The noCopy strategy you should know in Golang

In Go development, we often encounternoCopyThis structure is accompanied by a common annotation ‘must not be copied after first use’. This article will explore in depthnoCopyThe role of Go Vet and how it can help us avoid potential errors.

sync.noCopyThe role of

sync.noCopyStructures are usually associated withsync.WaitGroupWait for synchronization primitives to appear together, for example:

type WaitGroup struct {
    noCopy noCopy    state atomic.Uint64 // high 32 bits are counter, low 32 bits are waiter count.
    sema  uint32
}

noCopyThe structure itself is an empty structure, which implementsLockandUnlockMethod, both of these methods are null operations. It does not have actual functional attributes, but its existence is of great significance.

Go Vet and ‘Locks Erroneously Passed by Value’

Go Vet is a code checking tool that comes with the Go language, which can help us discover potential code errors.copylocksIt is a checker in Go Vet that checks if a structure containing a lock is incorrectly passed through a value in the code.

For example:

func func1(wg sync.WaitGroup) {
    wg.Add(1)
    // ...
    wg.Done()
}func func2() {
    var wg sync.WaitGroup
    func1(wg)
    wg.Wait()      //This will cause errors here     
}

stayfunc2In the middle, we passed it through valueswg, leading tofunc1In the middlewgIt has become a new copy. Whenfunc1In the middlewgimplementDone()During operation,func2In the middlewgStill isAdd(1)The state ultimately leads towg.Wait()Cannot function properly.

Go Vet’scopylocksThe checker will detect this error and prompt us with ‘Locks Erroneously Passed by Value’.

noCopyThe mechanism of action

noCopyStructure through Go VetcopylocksThe checker cooperates to prevent developers from mistakenly passing structures containing locks through values.

When a structure containsnoCopyWhen typing, Go Vet checks whether the structure has been passed through a value. If passed by value, Go Vet will issue a warning to remind developers that the structure should not be copied.

usenoCopyOther scenes

In addition to synchronization primitives,noCopyIt can also be used in other scenarios where copying needs to be prevented, such as:

  • Connection Pool: Connection pools usually need to avoid copying connection objects, as copying can cause connection failures.

  • Resource Management: Resource management objects typically need to avoid replication as it can lead to resource leaks.

  • Cache: Cache objects usually need to avoid copying, as copying can cause cache invalidation.

summary

noCopyIt is a simple mechanism, but it can effectively prevent developers from mistakenly copying structures containing locks or other important data, thereby avoiding potential errors.

When we seenoCopyWhen constructing a structure, one should be aware of its purpose and carefully examine the code to ensure that no structures containing locks or other important data have been copied incorrectly.

Back to Blog

Related Posts

View All Posts »
New package in Go 1.23: unique

New package in Go 1.23: unique

Go 1.23 introduces unique package for value normalization, enhancing memory efficiency and equality checks. Learn how "interning" works with unique and its benefits for Go developers.

How to cache well in Go

How to cache well in Go

Optimize Go app performance with caching strategies. Learn local vs distributed cache, memory management, and eviction policies. Enhance efficiency with Go's new unique package.

Modern LLM Basic Technology Compilation

Modern LLM Basic Technology Compilation

Explore the fundamentals of modern Large Language Models (LLMs) with an overview of Llama 3's training and architecture. Key points include pre-training data curation, model enhancements like GQA and KV Cache, and the importance of scaling laws in developing efficient LLMs.