Easily understand Go variables with visual examples. Go defines the variables as they read. Go is explicit and simple. Instead of doing the arithmetic of magic to read a C declaration, Go uses a simpler approach for us humans, not for aliens ( ). Clockwise/Spiral Rule I used to C’s declaration syntax after some time back then but that doesn’t mean we can’t make things better as in Go → C’s version: Clockwise/Spiral → Go’s version: Humanized A variable has a type, value and an address, . Variables are used when you need to store data somewhere to use it again for later operations in your code. as seen below Go is a statically-typed language. That means, every variable should have a type and all types are different from each other. What kind of information can be stored inside the variable. Type → The stored value inside the variable. Value → Where the variable can be found in computer memory. Address → Simply put, variables let us store data somewhere. Programming is all about transforming one form of data to another. The static-variables would be baked in our static programs. Without variables, there would be only static-data in our programs that we can’t change. , such as getting user feedback and store it into a variable, re-using the same value again, like in math PI number. We don’t have to type the values again and again thanks to variables. Variables let us do dynamic things If you would go into the machine-code level, which is the language that microprocessors understand ( , you would see that, there are many variable storage mechanisms that transform data from one form to another. So, basically, there’s a hardware-level support for variables in our computers. and some aliens) Before I show you how you can declare a variable, it’s better to talk about zero-values briefly which has a good importance in Go. Left side gets right side’s values if not initialized. If a variable is just declared and no value assigned to it, then Go will assign it a zero-value depending on the type of the variable. You can see which types get which zero-values by looking at the image above. This can come confusing to you at first, however, the rest of the notations except the first one are more like helper notations. The first one is the which uses the long declaration var keyword. The second one is the which guesses the variable’s type automatically. short declaration The third one is the , it’s like a mix of long and short declarations for multiple variables. multiple variable declaration And the last one is one-line . multiple short declaration Declares a variable named ageOfUniverse with a type of declare: var keyword declares a new variable. . Declare → var keyword declares a new variable ∑, ∫, 😱_)._ Name → The variable’s name is ageOfUniverse.This can be anything (including unicodes like π, but not this: Type → Which kind of data we can store inside of the variable.Here, we use int, because, we want to measure the age of the universe, maybe in years, approximately. When you just declare a variable by long declaration without assigning a variable to it, its value will be a zero-value. Zero-values are the defaults values in Go. Declares a variable named ageOfUniverse and stores a value of 14 billions in the variable and guesses its type automatically. Name → The name of the variable which is ageOfUniverse Declare & Assign → := is a special type of operator which declares and assigns a value to a variable. T_he data we want to store into the variable._ Value → The variable is declared and its value and type are assigned all together. You can assign any value that Go permits. Cool, yeah? Go can guess the type of the variable for you. This is called as and this type of declaration is called as . type-inferring short variable declaration or simply: short declaration When you use short declaration, Go will not use a zero-value assignment, obviously. Because, you directly assign a value to the variable, this will be its initial value. Though, you can assign a zero-value manually, as well. For example, you can assign 0 to an integer while you were still using the short-declaration ( ). 0 is a zero-value for integers By the way, 14e9 means, 14 billions. It’s called . Its default type is float64. “e” puts 9 zeros after 14. scientific-notation Declares multiple variables which have different types and values in the same statement. This is kind of a mix of the long and the short variable declaration notations. keyword declares a group of variables when used within paranthesis. var , ageOfUniverse int, is like a long declaration. Just declares a variable with a type you want and assigns it a zero-value automatically. The first line , is new to you, it’s called, . Just as with short declaration, it declares and assigns to the variables. Variable and value names are separated with commas. The second line multiple-variable declaration The number of items on the left-hand side ( should match to the number of items on the right-hand side ( . which is: livablePlanets, ageOfEarth) which is: 1, 4.5e9) No need to type, short-declaration operator here, “__:=”. is like the second line, however, it just declares variable and assigns it a string: . Notice that, when using it with one variable, there’s no need to use a commas. The third line coolLang go Declares two variables of type float64 and string; and assigns 14 billions to the first and “go” to the second variable. This is a simpler version of multiple-variable declaration which uses the short declaration. You just separate the variables and the values by commas. The number of variables and the values should match. Also called assigning data and = operator is used when assigning data to a variable. The variable should have been declared before and the data type should be the same with the variable’s declared data type. The variable will hold the new data that you set. However, when you want to assign wrong type of data to a variable, Go compiler will warn you and fail to compile. For example, you can’t assign a string to a variable which has a type of . integer Use long declaration, when you can’t know what data to store inside of the variable. Use short declaration, wherever you know what to store inside the variable. Use multiple declarations, well…, when you want to define multiple variables together. Also, use multiple declarations when you want to give a hint to other coders who read your code that you want to group this type of variables together. for other details. See this By the way, you can’t use short declarations outside of functions including main function. Or: you will meet with this error: “ syntax error: non-declaration statement outside function body”. When we declare an integer variable, Go reserves a 4-bytes of storage space in the memory for the variable. When the data doesn’t fit into the variable, Go compiler will warn you about that there’s an overflow and terminate. However, is a floppy type. Data it can store can change depending on the compilation. When compiling for a 32-bit architecture, it can hold 4-bytes, and in a 64-bit architecture, it can hold 8-bytes. int So, we can’t just simply store the age of the universe in an variable. We should have used instead, explicitly. Because, an can’t store ~14 billions of years, it can only store up to ~4 billions of years on 32-bit machines. Or it will overflow, but for a 64-bit machine, compilation will work. int uint64 int You can’t store 14 billions in a 4-bytes storage. Note: Universe was an infinitely small and dense dot once upon a “time” (Or wasn’t it? ?), however that doesn’t mean that we can do the same with Go! 😂 Ethan Siegel Try it yourself. : Example shows you how many bytes the ageOfUniverse variable can store. Play with it Incorrect code. Run it to see yourself. Correct-ish code. Run it. When you create a new variable, its life is limited in the scope which it was defined. We will talk about the scopes in much later tutorials. However, it’s good to know for now that, one of the important scopes that I want to tell you for now is that there is a package-level scope, and function-level scope. When we declare a new variable inside of a function, it can only be used inside that function ( . And, when we declare a new variable in the package scope, it can be seen and used by all the other package members unless we exported it to other packages. To define a variable in the package scope, you just need to use long declaration, you can’t use short declaration for package scope variables. unless we returned it from the function or write it to somewhere else) To export a variable to another packages we need to type the first letter of the variable’s name with an uppercase letter. However, I don’t recommend to expose your package variables to the outside world as much as you can. Even, it may be harmful to expose variables to the package. There are other ways to do that. You may read my post about , also read the part: “ ” in my another . encapsulation and packages Hide almost everything post OK, folks, that’s all for now. Thanks for reading! ❤️ ❤️ Share it on social media, . Please support me: Follow my publication and Add claps to my post ! spread the love 🐦 I’m mostly tweeting about Go, . you can follow me on twitter: @inancgumus You will receive notifications about my new posts and upcoming online Go course (every Wednesday). Click on the image to subscribe to “Learn Go Programming” email list! Thank you! Originally published at blog.learngoprogramming.com on October 4, 2017.