If a variable has an initial value, Go will automatically be able to infer the type of that variable using that initial value. It shows that age has been initialized with the value 29. The above program will print the following output. In the above program, age is a variable of type int and has initial value 29. Var age int = 29 // variable declaration with initial value The following is the syntax to declare a variable with an initial value. My age is 0ĭeclaring a variable with an initial valueĪ variable can also be provided an initial value when it is declared. In the above program, age can be assigned any integer value. My age is 0Ī variable can be assigned to any value of its type. If you run this program, you can see the following output. In this case, age is assigned the value 0, which is the zero value of int. If a variable is not assigned any value, Go automatically initializes it with the zero value of the variable's type. We have not assigned any value to this variable. The statement var age int declares a variable named age of type int. Var name type is the syntax to declare a single variable. There are various syntaxes to declare variables in Go. Variable is the name given to a memory location to store a value of a specific type. You can read Golang tutorial part 2: Hello World to learn about configuring Go and running the hello world program. This is the third tutorial in our Golang tutorial series and it deals with variables in Golang.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |