Skip to content

proposal: Go 2: NoCopy: official support in compiler (not just vet linter) #23764

Closed
@ldemailly

Description

@ldemailly

NoCopy official support in compiler (not just vet linter)

This is essentially a repeat of #8005 which got lost through FrozenDueToAge bot

(go versions not relevant - still an issue in go 1.9 and 1.10rc)

What did you do?

I wrote code that has a channel in a struct and got copied accidentally and then closed twice resulting in panic

What did you expect to see?

A way to detect accidental copies for objects that contain resources that need to stay unique
(or alternatively not getting panic when closing a channel more than once, but that's a different can of worm)

What did you see instead?

panic and time spent debugging until we found the trick

type noCopy struct{}
func (*noCopy) Lock() {}

type MyStruct struct {
  noCopy noCopy
  // ...

in #8005 (comment) (thanks for that!)

I believe every go project shouldn't rediscover and reimplement this and more importantly, one would want to get the detection at compile time instead of relying on a linter

while I'm here can I push my luck and ask for const ;-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions