Avatar ·

Pointers vs Values: when to use * and & in Go parameters and return values?

I just started learning Go and I keep getting confused about when to pass a pointer and when to pass a value. I've read a bunch of articles, but in practice I still get it wrong.\n\nFor example, I have a User struct and a function that updates it:\n\n```go\ntype User struct {\n Name string\n Age int\n Score float64\n}\n\nfunc UpdateScore(u User, newScore float64) {\n u.Score = newScore\n}\n```\n\nWhen I call `UpdateScore(myUser, 95.5)`, after the call `myUser.Score` remains unchanged. I understand that this is because Go copies the struct. So I change it to `*User`:\n\n```go\nfunc UpdateScore(u *User, newScore float64) {\n u.Score = newScore\n}\n```\n\nNow it works. But when I write a function that returns a new user, I see two options:\n\n```go\nfunc NewUser(name st