Translate

Variables in Go

Variables in Go

Index

How to Declare Variables in Go

In Go, the var keyword is used to declare variables. You can specify its type while declaring, or Go can infer the type automatically.

Syntax:

var variableName type

Example:

var name string    // Declaring a string variable
var age int        // Declaring an integer variable

Variable Initialization

In Go, you can assign an initial value while declaring a variable. If you don't assign a value, Go automatically assigns default values.

Example: Initialization

var name string = "Alice"  // Assigning value with declaration
var age int = 25           // Initializing an integer variable

Default Values in Go

If you don't initialize variables, they are assigned default values:

  • The default value of string is: "" (empty string)
  • The default value of int is: 0
  • The default value of bool is: false

Example: Default Values

var salary int  // Default value of int is 0
fmt.Println(salary)  // Output: 0

No comments:

Understanding the Meaning of "GO" in Hindi | Go in Hindi

Understanding the Meaning of "GO" in Hindi ➡ Hello and Welcome to All Our Visitors from around the world! We’re excited to hav...