Skip to content

proposal: Go 2: non-copyable structs by default #30450

Closed
@taralx

Description

@taralx

Most structs are actually not safe to copy. Most code handles structs by pointer because it is the best way to avoid accidentally copying it. (And there's hard-coding to prevent sync.Mutex being copied because of how often that mistake got made!)

Go 2 should accept this reality and make structs non-copyable by default. The copy() method could be extended to allow struct copying, and people who really want copyable structs can provide a method to call copy() for you.

Note that this doesn't stop moves of structs, e.g. returning them. Accepting them by value is a more complex question, since it introduces the possibility of an "empty" variable in many cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions