Is a struct actually copied between goroutines if sent over a Golang channel?

The Go Programming Language Specification

Send statements

A send statement sends a value on a channel. The channel expression must be of channel type, the channel direction must permit send operations, and the type of the value to be sent must be assignable to the channel's element type.

It's a copy because the value is sent to the channel by assignment to the channel's element type. If the value is a struct, then the struct is copied. If the value is a pointer to a struct, then the pointer to the struct is copied.


Yes, everything is a copy in Go, you can easily work around that by changing the channel to use a pointer (aka chan *largeStruct).

// demo: http://play.golang.org/p/CANxwt8s2B

As you can see, the pointer to v.buf is different in each case, however if you change it to chan *largeStruct, the pointers will be the same.

@LucasJones provided a little easier to follow example: https://play.golang.org/p/-VFWCgOnh0

As @nos pointed out, there's a potential race if you modify the value in both goroutines after sending it.