[Author: @mdimamhosen Date: 2025-04-19 Category: e.g., interview-qa/topic_name Tags: [go, concurrency, channels] ]
Variables are containers for storing data values
In Go, there are two ways to declare a variable
1. Use the var keyword, followed by variable name and type
package main
import ("fmt")
var name = "John Doe" // Variable One
func main() {
var fruit = "Apple" // Variable Two
fmt.Println(name)
fmt.Println(fruit)
}
2. Use the := sign, followed by the variable value
package main
import ("fmt")
func main() {
varOne := 100
varTwo := 2
fmt.Println(varOne)
fmt.Println(varTwo)
}
Frequently Asked Questions (FAQs)
1. How do you declare a constant variable in Go?
Answer: Use the const
keyword to declare a constant variable. Constants cannot be changed after they are declared.
package main
import ("fmt")
func main() {
const pi = 3.14
fmt.Println(pi)
}
2. Can you declare multiple variables in a single line?
Answer: Yes, you can declare multiple variables in a single line using the var
keyword or :=
syntax.
package main
import ("fmt")
func main() {
var x, y, z int = 1, 2, 3
fmt.Println(x, y, z)
}
3. What is the zero value of a variable in Go?
Answer: Variables in Go are automatically assigned a zero value if not explicitly initialized.
package main
import ("fmt")
func main() {
var number int
fmt.Println(number) // Output: 0
}
4. How do you declare a global variable in Go?
Answer: Declare the variable outside of any function.
package main
import ("fmt")
var globalVar = "I am global"
func main() {
fmt.Println(globalVar)
}
5. Can you reassign a value to a variable declared with var
?
Answer: Yes, variables declared with var
can be reassigned.
package main
import ("fmt")
func main() {
var name = "John"
name = "Doe"
fmt.Println(name)
}
6. What happens if you declare a variable but do not use it?
Answer: Go does not allow unused variables and will throw a compile-time error.
package main
func main() {
var unusedVar = 10 // This will cause an error
}
7. How do you declare a variable with a specific type?
Answer: Use the var
keyword followed by the type.
package main
import ("fmt")
func main() {
var age int = 25
fmt.Println(age)
}
8. Can you declare a variable without initializing it?
Answer: Yes, but it will have a zero value.
package main
import ("fmt")
func main() {
var name string
fmt.Println(name) // Output: ""
}
9. How do you declare a variable in a block?
Answer: Use the var
keyword inside a block.
package main
import ("fmt")
func main() {
var (
x int = 10
y string = "Hello"
)
fmt.Println(x, y)
}
10. What is the difference between var
and :=
?
Answer: var
can be used globally and allows explicit type declaration, while :=
is shorthand for local variable declaration and type inference.
package main
import ("fmt")
func main() {
var name string = "John" // Explicit type
age := 30 // Type inferred
fmt.Println(name, age)
}