* roger peppe <rogpe...@gmail.com> [191028 04:49]: > On Sun, 27 Oct 2019, 02:52 Marvin Renich, <m...@renich.org> wrote: > > I strongly encourage you to use > > > > var fset = token.NewFileSet() > > > > rather than > > > > fset := token.NewFileSet() > > > > Those two alternatives do exactly the same thing AFAIK. How is the latter > vulnerable to shadowing bugs where the former isn't?
In this case they are identical. It is the multi-variable "perhaps" redefinition that is the problem. Making a habit of using "var" whenever possible allows the compiler to call out where you have unintentionally shadowed one of several variables. With the multi-variable short declaration, you cannot tell which variables are being declared and which variables are "along for the ride" without _careful_ examination of the code all the way back to the beginning of the block. If you use "var", it is explicit at that statement. blk, msg, err := widget.Frabulate(arg) Which of blk, msg, and err are being newly declared here? You cannot tell. var blk Thingle var msg string blk, msg, err = widget.Frabulate(arg) Absolutely no question here; no need to examine earlier code. And if msg had been used earlier in this block, you would be told. This also has the distinct advantage that now the types of blk and msg are explicit. In the first case, someone reading the code would first have to determine what widget is (package or var) and then find the declaration of Frabulate to determine the types. I assert that in a large codebase, the second form is significantly easier to maintain than the first. Furthermore, := only saves three characters over using var if you are not mixing new declarations with simple assignment. This is insignificant. And var has significantly less cognitive load for those of us who have used many languages over the years, where := does not have a universally accepted meaning across languages. And, yes, I am aware that there are cases where you must use ":=". I wish that the design of the multi-variable short declaration had not only provided for but required that the variables that the programmer intended to declare were explicitly identified. Perhaps Go2 will address this. ...Marvin -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/20191028130952.mgfqdksai26atas2%40basil.wdw.