# Pitfalls With Closures In Go

DevFeed: [Pitfalls With Closures In Go](<https://devfeed.tech/articles/pitfalls-with-closures-in-go-22103.md>)

Original publisher: [Read original article](<https://www.ardanlabs.com/blog/2014/06/pitfalls-with-closures-in-go.html>)

Published: 2014-06-20T00:00:00Z

Content type: tutorial

Language: en

Sources: [William Kennedy](<https://devfeed.tech/sources/william-kennedy.md>)

Topics: [Go Language](<https://devfeed.tech/topics/go-language.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [ardan-labs](<https://devfeed.tech/tags/ardan-labs.md>), [blog](<https://devfeed.tech/tags/blog.md>), [closure](<https://devfeed.tech/tags/closure.md>), [code](<https://devfeed.tech/tags/code.md>), [function](<https://devfeed.tech/tags/function.md>), [go](<https://devfeed.tech/tags/go.md>), [go-programming](<https://devfeed.tech/tags/go-programming.md>), [golang](<https://devfeed.tech/tags/golang.md>), [pitfalls](<https://devfeed.tech/tags/pitfalls.md>), [programming](<https://devfeed.tech/tags/programming.md>), [range](<https://devfeed.tech/tags/range.md>), [scope](<https://devfeed.tech/tags/scope.md>), [variable](<https://devfeed.tech/tags/variable.md>)

## AI overview

This tutorial explains how closures in Go can cause bugs when variables from a for-range loop are used inside goroutines. It examines which variables are passed as function parameters and which are captured through closures.

## Source excerpt

Introduction Closures in Go are a very powerful construct but they can also be the cause of bugs if you don't understand how they work. In this post I am going to pull a small piece of code from Chapter 2 from the Go In Action book that discusses a pitfall you can run into when using closures. The full code example can be found in the Github repository for the book. Chapter 2 discusses this code example in full detail. The Closure Pitfall First let's look at the piece of code: