- Is it possible to declare multiple variables in one var statement?
Yes, although in my personal experience I have never seen it locally. The var ( ... ) syntax is traditionally reserved for top-level declarations (although I have never seen this in any style guides, this is just my observation).
- Should you declare variables with var or deduce a type using: = okay?
Usually := excellent. Usually you want to use var with an explicit type when the type is either not obvious, or you want the variable to be something other than the obvious type, or even forced type casting.
For example, let's say you have a variable a , and you want to make a duplicate variable b . Sometimes you can remind the reader that type a , and not just do b := a .
As an example of a βdifferent than obviousβ case, imagine that you want a 16-bit floating point value. You can do one of the following, but some of them will consider a second cleaner:
a := float16(1) var a float16 = 1
Finally, use var , where you only declare without assigning a variable (for example, to use its default value or assign it later):
`var a int`
- When declaring a piece of coins, is it best to declare them locally in a function?
It mainly depends on two factors: is it logically connected with the function and is it bulky enough if declared locally?
Obviously, if the same value is used in other functions, you might want to declare it globally. Sometimes, even if it is used in only one function, it still makes more sense to declare it globally (for example, if it looks like a general package behavior parameter, for example, how many bytes are used for local storage or something like that).
Even if it is logically associated with a local function, if it is a very long value, declaring it locally can significantly clutter the code, which is a good reason for declaring it globally:
func myFunc () {
// do some things
mySlice: = [] string {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10" "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" 24 ", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37 "," 38 "," 39 "," 40 "," 41 "," 42 "," 43 "," 44 "," 45 "," 46 "," 47 "," 48 "49", "50 "," 51 "," 52 "," 53 "," 54 "," 55 "," 56 "," 57 "," 58 "," 59 "," 60 "," 61 "," 62 ", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73" 74 "," 75 ", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "90", "91", "90 "," 100 "," 101 "," 102 "," 103 "," 104 "," 105 "," 106 "," 107 "," 108 "," 109 "," 110 "," 111 ", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123" 126 ", β125β, β126β, β127β, β128β, β129β, β130β, β131β, β132β, β133β, β134β, β135β, β136β, β137β "," 138 "," 139 "," 140 "," 141 "," 142 "," 143 "," 144 "," 145 "," 146 "," 147 "," 148 "149", "150 "," 151 "," 152 "," 153 "," 154 "," 155 "," 156 "," 157 "," 158 "," 159 "," 160 "," 161 "," 162 "," 163 " , "164", "165", "166", "167", "168" 169 "," 170 "," 171 "," 172 "," 173 "," 174 "," 175 "," 176 " , "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", " 189 "," 190 "," 191 "," 192 "," 193 "194", "195", "196", "197", "198", "199", "200", "
// Do some other things.
}
(Note that I did not write this as a block of code because it would not have turned around differently and conveyed how ugly it is.)