alt
codecamp
Golang Essentials: Types and Variables
Previous
Next
☰
About
Profile
Account
Login
Sign Up
Logout
Run
Test
Mark Complete
## Types and Variables in Go Go is a statically typed language, which means that variables must be declared with a specific type. The basic types include `int`, `float64`, `bool`, and `string`. When you declare a variable, Go requires you to specify the type along with the variable's name. ### Declaring Variables Variables in Go can be declared using the `var` keyword. Here's the basic syntax: ```go var variableName type = initialValue ``` For example: ```go var age int = 30 ``` ### Short Variable Declarations Go also offers a shorthand for declaring and initializing a variable: ```go name := "John" ``` This statement is shorthand for `var name string = "John"`. ### Zero Values If you declare a variable without initializing it, Go automatically assigns a default zero value to it, depending on its type. For example, the zero value for `int` is `0`, for `string` it's `""` (empty string), and for `bool` it's `false`. ### Conclusion Understanding and using types and variables correctly is fundamental in Go programming. Remember to always specify the type when declaring a variable, or use the shorthand syntax for initialization.