GithubHelp home page GithubHelp logo

nuances-in-go-part-1's Introduction

Nuances in Go: Exploring the Tiny Details. Part-1.

I would like to shed light on the lesser-explored aspects of Go programming, those subtle behaviors that may have gone unnoticed, occasionally puzzled you, or even caused you to stumble upon solutions without fully grasping why they work as they do. Some of you might have even intuited these behaviors, finding them peculiar. Let's dive right into these nuances.

Arguments evaluation in range loops

We're all familiar with the range loop syntax in Go, which necessitates an expression, such as i, v:= range exp, where exp represents the expression. The important question here is how is this expression evaluated. Let me prove that you know it or otherwise, try answering the code question below, try it for real, it would make the reading a bit more interesting (I fear I might be a terrible writer);

    arrayOfInt := []int{5, 6, 7}
    for range arrayOfInt {
    arrayOfInt = append(arrayOfInt, 10)
    }
    fmt.Println(arrayOfInt) // what is our output?

You've got your output?, Excellent. Now, consider this similar piece of code:

    arrayOfInt := []int{5, 6, 7}
    for i := 0; i < len(arrayOfInt); i++ {
    arrayOfInt = append(arrayOfInt, 10)
    }
    fmt.Println(arrayOfInt) // What do you expect the output to be?

What do you think will be different between these two code snippets? I sincerely hope you haven't run them on your system yet (especially the last one).

The respective outputs are:

{5, 6, 7, 10, 10, 10}

and

(No output; an infinite loop)

Isn't it intriguing that two seemingly similar tasks behave so differently? Which of these codes do you find more unexpected or peculiar? Let's explore these nuances further, especially the range. This is the jinx of the range loop;

It's important to note that when utilizing a range loop, the specified expression is evaluated only once, before the loop's initiation. In this context, "evaluation" refers to the process of duplicating the provided expression into a temporary variable, which is then iterated upon by the range loop. The range loop uses this temporary variable.

The temporary slice used by range remains a three-length slice. Hence, the loop completes after three iterations. Returning to the topic of the range operator, it’s crucial to understand that the behavior we’ve discussed — where the expression is evaluated just once — applies to Channels and Arrays as well.

In the scenario of the classic for-loop, the loop never terminates. This is because the len(arrayOfInt) expression is re-evaluated during each iteration, and since we continually add elements, we never reach a termination condition.

I’ll say it is pretty crucial to be aware of this distinction to employ Go loops effectively, Period.

Before I go, I’d like to suggest a book for your next reading choice, Domain-Driven Design with Golang by Matthew Boyle. It’s a great read, and I’m starting it too. Stay tuned, as I’ll be sharing insights from it with you soon.

nuances-in-go-part-1's People

Contributors

tobigiwa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.