Understanding Pointers in Go (Especially If You’re Coming from JavaScript)

If you are coming from Javascript or Typescript, pointers in Go can seem overwhelming or unnecessary at first. Not because they are hard, but because it's not obvious why they matter. In Javascript...

By · · 1 min read
Understanding Pointers in Go (Especially If You’re Coming from JavaScript)

Source: DEV Community

If you are coming from Javascript or Typescript, pointers in Go can seem overwhelming or unnecessary at first. Not because they are hard, but because it's not obvious why they matter. In Javascript, you don't think about memory management. You just pass objects around, and voilà, everything works. So when you start seeing ***** and & everywhere in Go, it can start to feel a bit extra and intimidating. This was my experience too. I had a good understanding of what pointers are, but I never really got why I should care. So in this article, I’ll walk through what pointers are, how they work, and when they actually make sense to use, in a simple and easy-to-follow way. Let’s start with a simple struct type Person struct { Id string `json:"id"` Name string `json:"name"` Age int `json:"age"` } var persons = []Person{ {Id:"5f919d2c-3536-4c44-8f45-ab420e089827", Name:"Promise", Age:25}, {Id:"dcb70a59-2eb2-4bd4-a1e9-d7fcc089bfeb", Name:"Ben", Age:29}, {Id:"f73dd3c8-5cf4-4075-b65e-1b19991fdb