Home » Software Development » Golang – Smarter containers

About George Aristy

George Aristy
Experienced software engineer currently working in decentralized identity management, and with an extensive history providing service-oriented solutions in the telecom industry.

Golang – Smarter containers

This post is part of a series where I do my best to organize my thoughts around Go: its paradigms and usability as a programming language. I write this as a Java programmer that respects the principles of Elegant Objects.

I wish to make a correction in this post. I want to focus on this statement:

Products is not a “smart” container – see point #3 in the Java proposal. You would have to manually append the newly-created Product to Products

What I meant is that clients would have to use it like this:

prods := make(Products, 0)
p := prods.Create(10)
prods = append(prods, p)    // extra imperative code forced on the client to add the product to the container

Let’s pay attention to this snippet:

func (p *Products) Create(price float64) Product {
    prod := ∏uct{id: 123, price: price}
    tmp := append(*p, prod) // compiler would not allow p = &(append(*p, prod))
    p = &tmp                // the problem here is that the caller still retains the original handle to `p`
    return prod

I was really close to solving that riddle. The trick is to assign a new value to the pointer variable. The pointer variable itself is passed by value, so callers would also see the side effects. Here’s what I mean:

func (p *Products) Create(price float64) *Product {
    prod := ∏uct{price: Price}
    *p := append(*p, prod) // done in one line for brevity
    return prod
// A test like this would pass
func TestCreate(t *testing.T) {
    prods := make(Products, 0)
    prod := prods.Create(10)
    assert.Len(t, prods, 1)
    assert.Contains(t, prods, prod)

With this I’ve proved that Products can be made smarter: create products and dynamically append them to itself.

Several problems remain:

  • Slices don’t know how to iterate themselves – only range knows that. Since this power is taken away from developers, iteration of Products is only possible with objects in memory. You cannot implement a custom iterable – like in Java – that can dynamically fetch results from a datasource.
  • Since iteration is only done in memory space, deferred execution is harder to pull off. You’d basically need an abstraction for a function that returns the actual slice (think type Products func() []Product)
  • Cannot be decorated. Cannot implement Premium as a slice of products because the type will have no usable attribute for this. Unless… we go back the function abstraction idea…

Published on Java Code Geeks with permission by George Aristy, partner at our JCG program. See the original article here: Golang – Smarter containers

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Notify of