Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cuddle function call expression on var declared in line above #138

Closed
sveinnthorarins opened this issue Sep 29, 2023 · 2 comments
Closed

Comments

@sveinnthorarins
Copy link

Hey, thanks for your work.

I'm getting the "Expressions should not be cuddled with declarations or returns" message on line 2 in the following example:

var wg sync.WaitGroup
wg.Add(2)

Since I'm performing an action (calling a function) on the variable declared in the line above I feel like cuddling should be okay and this shouldn't give off a warning.

Thoughts?

@bombsimon
Copy link
Owner

I think var is something that should be used to group and separate setup/declarations in functions or in a global scope and be separated from the rest of the codebase. However there are two easy ways to solve this.

  1. Use -allow-cuddle-declarations
  2. Use an assignment instead of a declaration
wg := sync.WaitGroup{}
wg.Add(1)

@sveinnthorarins
Copy link
Author

Okay, your reasoning makes sense to me so I'm going to try to follow it and will just go with the 2. way (assigning instead of declaring).

Somehow I had imprinted in me that empty structs should be declared with var but from what I can gather there's no difference between declaring like that vs. assigning and initializing an empty struct.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants