Quantcast
Channel: How to write idiomatic constructor - Stack Overflow
Viewing all articles
Browse latest Browse all 4

How to write idiomatic constructor

$
0
0

I'm confused about the constructors in Go. Most constructors I've seen return a struct, but 'Effective Go' suggests that an interface can be returned in some cases, according to the rule of 'Generality'.

I trust 'Effective Go' to provide good ideas, but this doesn't seem to follow the principle of 'accept interfaces, return structs'. I guess that many types implement an interface and nothing more than that, so in that case it would be common to see constructors which return interfaces.

Another related statement is that interfaces should be defined by the consumer, but 'Generality' means that the interface is defined by the producer.

Can someone clarify?


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>