In Go, variables are used to store and manipulate data. They hold a value that can be of a specific data type, such as integer, string, boolean, or a custom-defined type. Variables in Go have a declared type, and their values can be modified during the program execution.
Here’s how you can declare and use variables in Go:
package main
import "fmt"
func main() {
// Declare a variable and initialize it with a value.
var age int = 25
fmt.Println("Age:", age)
// Declare multiple variables of the same type.
var name, city string = "John", "New York"
fmt.Println("Name:", name)
fmt.Println("City:", city)
// Infer the variable type with shorthand declaration.
country := "USA"
fmt.Println("Country:", country)
// Declare variables without specifying the type (type inference).
var score = 95
fmt.Println("Score:", score)
// Declare variables without initialization (zero value).
var count int
var isActive bool
fmt.Println("Count:", count)
fmt.Println("IsActive:", isActive)
// Assign a new value to an existing variable.
count = 10
isActive = true
fmt.Println("Updated Count:", count)
fmt.Println("Updated IsActive:", isActive)
// Declare and initialize variables in a single line (type inference).
quantity, price := 5, 9.99
fmt.Println("Quantity:", quantity)
fmt.Println("Price:", price)
}
In the above example:
- We declare a variable
age
of typeint
and initialize it with the value25
. - We declare multiple variables
name
andcity
of typestring
and initialize them with values"John"
and"New York"
, respectively. - We use the shorthand declaration
:=
to infer the type of the variablecountry
and initialize it with the value"USA"
. - We declare a variable
score
without specifying the type and initialize it with the value95
. Go infers the type based on the value assigned. - We declare variables
count
of typeint
andisActive
of typebool
without initialization. They will have their zero values (0
andfalse
, respectively). - We assign new values to the existing variables
count
andisActive
. - We declare and initialize variables
quantity
andprice
of inferred types (int
andfloat64
, respectively) in a single line.
By using variables, you can store and manipulate data in your Go programs. Variables provide flexibility and allow your program to work with different values throughout its execution.
Declaring a single variable
In Go, you can declare a single variable using the following syntax:
var variableName dataType
Here’s an example of declaring a single variable in Go:
package main
import "fmt"
func main() {
var age int
age = 25
fmt.Println("Age:", age)
}
In the above example, we declare a variable named age
of type int
using the var
keyword. The int
type represents integers. After declaring the variable, we assign the value 25
to it using the assignment operator =
. Finally, we print the value of the age
variable using fmt.Println()
.
You can also declare and initialize a single variable in a single line using the shorthand declaration:
variableName := initialValue
Here’s an example:
package main
import "fmt"
func main() {
name := "John"
fmt.Println("Name:", name)
}
In the above example, we declare a variable named name
and initialize it with the value "John"
using the shorthand declaration. Go infers the type of the variable based on the value assigned. Finally, we print the value of the name
variable.
By declaring single variables in Go, you can store and manipulate data within your program.
Multiple variable declaration in Go
In Go, you can declare multiple variables in a single statement. Here’s the syntax for declaring multiple variables:
var (
variable1 dataType1
variable2 dataType2
// ...
)
You can also initialize these variables with their respective initial values in the same statement:
var (
variable1 dataType1 = value1
variable2 dataType2 = value2
// ...
)
Here’s an example that demonstrates multiple variable declaration in Go:
package main
import "fmt"
func main() {
var (
name string = "John"
age int = 25
isActive bool = true
)
fmt.Println("Name:", name)
fmt.Println("Age:", age)
fmt.Println("Active:", isActive)
}
In the above code, we declare three variables name
, age
, and isActive
in a single var
statement. Each variable is assigned its respective data type and initial value. The name
variable is of type string
and initialized with the value "John"
. The age
variable is of type int
and initialized with the value 25
. The isActive
variable is of type bool
and initialized with the value true
. Finally, we print the values of these variables using fmt.Println()
.
Using multiple variable declaration can help make your code more concise and readable, especially when you have a group of related variables that you want to declare together.
Short hand variable declaration In Go
In Go, you can use the shorthand variable declaration :=
to declare and initialize variables in a single line. This shorthand declaration automatically infers the variable’s type based on the assigned value.
Here’s the syntax for the shorthand variable declaration:
variableName := initialValue
Here’s an example that demonstrates the shorthand variable declaration in Go:
package main
import "fmt"
func main() {
name := "John"
age := 25
isActive := true
fmt.Println("Name:", name)
fmt.Println("Age:", age)
fmt.Println("Active:", isActive)
}
In the above code, we use the shorthand declaration :=
to declare and initialize variables name
, age
, and isActive
in a single line. The types of these variables (string
, int
, and bool
) are inferred based on the assigned values.
The shorthand declaration saves you from explicitly specifying the variable types, making the code more concise. It is particularly useful when you want to declare and initialize variables quickly without repetitive type annotations.
One important thing to note is that the shorthand declaration can only be used inside functions. It cannot be used at the package level for declaring global variables.
Additionally, when using the shorthand declaration, at least one of the variables on the left side of :=
must be new (i.e., not previously declared in the same scope). If you want to reassign a value to an existing variable, you should use the regular assignment operator =
instead of :=
.
Overall, the shorthand variable declaration is a handy feature in Go that simplifies the process of declaring and initializing variables in a single line.