Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 10
either comment the line of variable declaration, or assign a zero element to an array slot:
matrix[0][0] = 0
that will not change anything (only replacing a zero value by a zero value), and avoid the error message ^^
however, adding a double slash at start of array declaration would be more "clean", without deleting it: you only need to remember to uncomment the line when you need to use the variable ;P
+ 4
U know, in this way, golang makes ur code ready for production, and if u declare or import anything, u have to use it, golang has an optimizer inbuilt, though it can sometimes annoy u, we can either remove the declaration/import or comment it.
+ 4
King I never said to assign 0 to a [0][0] matrix, but to assign a slot of the declared matrix... and give example of assigning the first slot (in OP use case it's a 2d array, so I assign 0 to matrix[0][0]: this is perfectly valid...)
I also said "either comment OR assign"... so by commenting, you implicitly doesn't need the dummy assignement ;P
The only new thing you showed in your code is the use of the implicitly declared _ global variable, commonly used where we don't need the value produced by an expression (such as index in a for loop used with a range).
+ 3
King what is not going to help anyway?
I just suggested almost the same things as you: comment or do a dummy assignement ^^
+ 2
I think it's a code smells protection. If you dont use it, why declare it?
You can always use underscore identifier to bypass this unassigned error
_ = matrix
+ 1
Maybe it seems crazy what I'm going to say, but P.M. is completely right, that error was intentionally added to force us as programmers to write a better code, and GO is not the only one who gives us an error or warns us about the declaration of unused variables, programming languages as C or C++ warns us about about unsed variables too. Before upvoting or downvoting, please, check my solution to that error in the comments section of ChillPill 's code
+ 1
Hacker-KR4636 the difference with C/C++ is that GO produce an error and doesn't allow to run the program, while C/C++ only produce a warning at compile time, without avoiding program compilation/execution...
also, your solution in comment section of OP's code just do a dummy assignement as stated in King's now deleted answer/code, using the _ implicitly declared global variable ^^
+ 1
visph you're right in everything, GO produces an error and noy a warning, and my solution is not a real solution, but is not dummy, because it works, and the real thing is that, if you declare a variable, it supposed you will be using it in your program.
And my solution isn't the only one, thre are others, for example, there are some modules available in github to resolve that problem
+ 1
Hacker-KR4636 I use "dummy" as that was the term used by King... as I understood it was "unuseful" or "useless"... as was the solution I provided in my first answer: set a slot of the array to zero (or comment the variable declaration), just for let the interpreter guess that the variable was used ^^ (in GO all variables are default initialized to their zero value)
+ 1
David Ordás that's what I'm saying, but it seems no one understood my answer, if you want to declare a variable that you know you will be using it through your program, you can use the underscore, after all, you will be using it later in the program.
- 1
Looper🇮🇳 well that's intentionally added so that's not a compiler's problem