
var keyword. The general steps involved in variable declaration include:
- Using the
varkeyword. - Specifying the variable name.
- Declaring the variable’s data type.
- Using the assignment operator (
=) to assign a value.
Declaring a String Variable
To declare a string variable, use the following syntax:s is assigned the string value “Hello world”.

Declaring an Integer Variable
Similarly, you can declare an integer variable as shown below:i is of type int and stores the value 100.
Declaring a Boolean Variable
For Boolean values, thebool data type is used. Always write Boolean literals in lowercase:
b with the value false.
Declaring a Float Variable
Go supports two floating-point types:float32 and float64. The following declaration uses float64:
f of type float64 set to the value 77.90.
Variables in Go can also be declared without an initial value. In such cases, Go automatically assigns a zero value based on the type. For example, an uninitialized
int variable gets a value of 0.Running a Simple Example
Below is a complete program that demonstrates variable declaration and usage within themain function. This example uses the fmt package to print a variable’s value.
greeting is assigned the value “Hello World”, which is then displayed in the console.
That concludes the discussion on variable syntax and declaration in Go—an essential concept that underpins building robust Go applications. We’ll see you in the next lesson.