Consider this case: package main
import ( "errors" "fmt" "sync" ) type A struct { Lock sync.Mutex X int } type B struct { A Y int } func newA(x int) (A, error) { if x > 0 { return A{}, errors.New("x should not be positive") } return A{X: x}, nil } func newB(x int, y int) (B, error) { a, err := newA(x) if err != nil { return B{}, err } return B{ A: a, Y: y, }, nil } func main() { b, err := newB(-1, 3) fmt.Println(b.Y) fmt.Println(err) } Actually, there is no way to avoid struct copy if the struct containing mutex is in another struct directly. Run go tool vet test.go It will reports test.go:32: literal copies lock value from a: main.A contains sync.Mutex On Tuesday, December 13, 2016 at 3:16:14 AM UTC+8, Ian Lance Taylor wrote: > > On Sun, Dec 11, 2016 at 11:48 PM, <i...@liangsun.org <javascript:>> > wrote: > > > > What about type embedding? If put a sync.Mutex in a struct, that struct > can > > not be embedded into another struct, right? > > That turns out not to be the case. You can embed that struct in > another struct. But then, of course, you can not simply copy that > other struct. > > > So I guess it's better idea to use structs with pointer to mutex. > > It depends. > > Ian > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.