From the course: Learning Go

Declare and initialize variables - Go Tutorial

From the course: Learning Go

Declare and initialize variables

- [Instructor] Go is a statically typed language. You have to set the type of each variable during the compilation process and you can't change variable types at runtime but with go syntax you can set types either explicitly or implicitly. And there are a couple of variations in syntax you can use. I'm going to create a new folder within my workspace, and I'm going to call it practice. And from now on most of these lessons will be in the practice folder. And as I did before I'm going to start with a main dot go file. So I'll copy this main dot go file from hello and I'll paste it into practice. I'll make sure I'm working in the right version. And now I'll add code to the main function. I'm going to clear the console so we know for sure we're running the current code. Here's one way of declaring a variable in go, I'll start with the keyword var and I'll create a variable named a string to set its type explicitly declare the type here after the variable name and then optionally you can assign it an initial value. Then I'll change this call to the print line function from hello from go to the variable of string. Now I'll run the application within visual studio code. I'll go to the menu and choose run, run without debugging in the future. I'll just use the keyboard shortcut. And I see the output this is go, I can also find out what the type of the variable is by using the print F function And I'll pass in the variables type is, and then I'll use what's known as a verb or a placeholder percent uppercase T. That means the current variables type. And I'll pass in that variable as a second argument. And I see that the variables type is string. Now let's do that with a couple of other types. I'll create an integer, I'll name it an integer very creative. I'll set t's typed to int, and I'll give it a value of 42 I'll copy and paste my call to print line. And then I'll output an integer and notice that I get the value 42, but it's concatenated to the previous output. That's because I forgot to put in a line feed. So I'll add the line feed right there. Print F doesn't do the line feed automatically. I'll run the code again. And now the outputs are separated. Every type has a default value numeric types default to zero. I'll create another variable that I'll call default int. I'll set its type as int but I won't assign an initial value. Then I'll copy and paste the call to print line and output default int. And I see that the value is zero. So that's all explicit typing. I'm saying this is the variable, and this is it's type for implicit typing the variable types are inferred based on initial values that you assigned. So I'm going to create another string. I'll call it another string and I'll give it a value of this is another string. I'll copy and paste these two lines of code. I'll change the name of the variable that I'm looking at. And I see this is another string and the variables type is string. The compiler figured out the type for me. I'll copy all three of these lines of code and create a new section. This one will be another int and I'll give it a value of 53. And as long as I don't have a fraction at the end that is no decimal dot this will be an integer and I'll pass in another int into both of these calls. And I see that the variables type is int there's another style you can use for initializing variables using an operator called colon equals. If you use colon equals you don't use the var keyword I'll create a variable called my string and assign it with colon equals. This is also a string. I'll copy and paste these lines change the variable references. And I'll run the code. And I see the correct output down at the bottom. The colon equals operator only works for variables inside functions. If you declare variables outside of functions you must use the var keyword. And that's also true for a special kind of variable called constant. Constant can be declared outside of functions like this. You start with the const key word, I'll call this a const. And once again, the type can either be explicit or implicit. I'll create an explicit type and I'll give it a value of 64. That constant is now available to any function within this file. If I used an upper case initial character it would be public I'll copy and paste my lines of code to output the value and the type of a variable and change the variable references. And I'll run the code. And I see that 64 is an integer. So those are the different ways that you can create and initialize variables and go either with explicit or implicit typing and either with the colon equals operator but only inside a function or with the var keyword.

Contents