Avatar ·

Difference between := and = in Go: short declaration vs assignment

Hello everyone! I just started learning Go and got confused about the := and = operators. They both seem to be used for working with variables, but what's the difference?\n\nHere's an example that confused me:\n\n```go\npackage main\n\nimport "fmt"\n\nfunc main() {\n name := "Anna" // this works\n var age int = 25 // this also works\n age = 30 // and this works\n // name = "Boris" // I tried this too\n}\n```\n\nWhen I write name := "Anna" — it's fine. But if I first declare a variable with var, and then try to use := with the same variable, the compiler complains: "no new variables on left side of :=". Why?\n\nAlso I noticed that := only works inside functions. If I write this:\n\n```go\nvar version = "1.0" // globally — ok\n// version := "1.0" // globally — error!\n```