Avatar ·

Correct way to initialize an empty slice in Go: var vs make

Hello everyone! I just started learning Go and encountered confusion when initializing empty slices. In the documentation and examples, I see two main approaches, but I don't understand which one is correct and in which situations.\n\nThe first way is through variable declaration:\n```go\nvar s []string\n```\n\nThe second is through the make function:\n```go\ns := make([]string, 0)\n```\n\nI read that both create an empty slice, but is there a difference between them? When I use `var s []string`, I can check `s == nil` and get `true`. But when using `make([]string, 0)` — I get `false`. Does this mean one way is better than the other?\n\nI'm especially concerned about the situation when I create a slice that will later be filled with data, for example, when parsing JSON or reading lines from a file. I want to avoid unnecessary memory allocations.