There is no native way to disable copying in Go. So if you have a structure that you want the user to not be able to copy, but only pointer pass to ensure global uniqueness, you can do that by defining a structure called noCopy and implementing the sync.Locker interface.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// noCopy may be embedded into structs which must not be copied
// after the first use.
//
// See https://golang.org/issues/8005#issuecomment-190753527
// for details.
type noCopy struct{}

// Lock is a no-op used by -copylocks checker from `go vet`.
func (*noCopy) Lock() {}
func (*noCopy) UnLock() {}

Then embed noCopy into your custom structure and go vet will do the checking for us.

As an example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package main

import (
    "fmt"
)

type noCopy struct{}

func (*noCopy) Lock()   {}
func (*noCopy) Unlock() {}

type Demo struct {
    noCopy noCopy
}

func Copy(d Demo) {
    CopyTwice(d)
}
func CopyTwice(d Demo) {}

func main() {
    d := Demo{}
    fmt.Printf("%+v", d)

    Copy(d)

    fmt.Printf("%+v", d)
}

The execution results are as follows.

1
2
3
4
5
6
7
8
$ go vet main.go
# command-line-arguments
./main.go:16: Copy passes lock by value: main.Demo contains main.noCopy
./main.go:17: call of CopyTwice copies lock value: main.Demo contains main.noCopy
./main.go:19: CopyTwice passes lock by value: main.Demo contains main.noCopy
./main.go:23: call of fmt.Printf copies lock value: main.Demo contains main.noCopy
./main.go:25: call of Copy copies lock value: main.Demo contains main.noCopy
./main.go:27: call of fmt.Printf copies lock value: main.Demo contains main.noCopy