FAQ
type X struct {
     Value int
     sync.Mutex
}

If I understand you correctly, you want to avoid having to type out the
"new" for the mutex each time you use the type literal. This should work,
shouldn't it?
On Thursday, August 27, 2015 at 3:48:42 PM UTC-7, Gbr wrote:

Trying to construct a type with an embedded field where the type also
implements an interface:


type X struct {
*sync.Mutex
Value int
// other named fields
}

type Y interface {
sync.Locker
// other method signatures properly implemented by X
}

/* Compile-time implementation prover */
var _ Y = X{}


The sync.Locker requires a pointer receiver - as other interfaces do.
This can be satisfied by X embedding the sync.Mutex as a pointer field.
The question is then initialization.

This works:

var _ Y = X{ new(sync.Mutex), 0}

but becomes clumsy as the number and complexity of fields in X increases
(not to mention error prone as the order and type of fields in X are
edited).

Naming *sync.Mutex in X does not work, as then Y is not implemented.

How then to initialize X or otherwise allow a direct embedding of
sync.Mutex in X while implementing Y?

Thanks...
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 19 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 27, '15 at 10:48p
activeAug 29, '15 at 1:56a
posts19
users7
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase