Avatar ·

How to get rid of the "declared and not used" error in Go?

Hi everyone! I started learning Go a couple of weeks ago, and I keep getting the same error: "declared and not used". For example, I write simple code:\n\n```go\npackage main\n\nimport "fmt"\n\nfunc main() {\n x := 5\n y := 10\n z := x + y\n fmt.Println("Hello, World!")\n}\n```\n\nAnd I get a compilation error: `y declared but not used` and `z declared but not used`. I understand that Go is a strict language, but this is driving me crazy! Sometimes I declare a variable to use it later, but the compiler complains immediately.\n\nAnother example — I work with functions that return multiple values, but I only need one:\n\n```go\nresult, err := someFunction()\nfmt.Println(result)\n```\n\nAnd again the error: `err declared but not used`. I have to write `_ = err`, which feels like a workaround.\n\nI