Home »
Golang
Golang Multiple Variables (Constants) Declaration
By IncludeHelp Last updated : October 05, 2024
In Go programming language, there is another shorthand to declare multiple Variables (Constants) together.
Golang Multiple Variables and Constants Declaration
Variables are declared using the var keyword and constants are declared using the const keyword.
Both multiple variables & constants can be declared using the following syntax,
Multiple Variables Declaration
Syntax to declare multiple variables:
var (
name = value
name = value
name = value
...
)
Multiple Constants Declaration
Syntax to declare multiple constants:
const (
name = value
name = value
name = value
...
)
Practice the following programs illustrating the shorthand Declaring variables or constants of the same types or mixed types.
There are multiple ways to find the type of a variable/constant. In the below programs, we are using the %T format specifier to print the type of a variable/constants with the Printf() function of the fmt package.
Example 1: Declaring multiple variables of same type
// Golang program for Declaring multiple
// variables of same type
package main
import "fmt"
func main() {
var (
a = 10
b = 20
c = 30
)
// printing the values
fmt.Println("value of a: ", a)
fmt.Println("value of b: ", b)
fmt.Println("value of c: ", c)
// printing the types
fmt.Printf("Type of a: %T\n", a)
fmt.Printf("Type of b: %T\n", b)
fmt.Printf("Type of c: %T\n", c)
}
Output
value of a: 10
value of b: 20
value of c: 30
Type of a: int
Type of b: int
Type of c: int
Example 2: Declaring multiple variables of mixed type
// Golang program for Declaring multiple
// variables of mixed type
package main
import "fmt"
func main() {
var (
a = 10
b = 10.23
c = "Hello"
)
// printing the values
fmt.Println("value of a: ", a)
fmt.Println("value of b: ", b)
fmt.Println("value of c: ", c)
// printing the types
fmt.Printf("Type of a: %T\n", a)
fmt.Printf("Type of b: %T\n", b)
fmt.Printf("Type of c: %T\n", c)
}
Output
value of a: 10
value of b: 10.23
value of c: Hello
Type of a: int
Type of b: float64
Type of c: string
Example 3: Declaring multiple constants of same type
// Golang program for Declaring multiple
// constants of same type
package main
import "fmt"
func main() {
const (
a = 10
b = 20
c = 30
)
// printing the values
fmt.Println("value of a: ", a)
fmt.Println("value of b: ", b)
fmt.Println("value of c: ", c)
// printing the types
fmt.Printf("Type of a: %T\n", a)
fmt.Printf("Type of b: %T\n", b)
fmt.Printf("Type of c: %T\n", c)
}
Output
value of a: 10
value of b: 20
value of c: 30
Type of a: int
Type of b: int
Type of c: int
Example 4: Declaring multiple constants of mixed type
// Golang program for Declaring multiple
// constants of mixed type
package main
import "fmt"
func main() {
const (
a = 10
b = 10.23
c = "Hello"
)
// printing the values
fmt.Println("value of a: ", a)
fmt.Println("value of b: ", b)
fmt.Println("value of c: ", c)
// printing the types
fmt.Printf("Type of a: %T\n", a)
fmt.Printf("Type of b: %T\n", b)
fmt.Printf("Type of c: %T\n", c)
}
Output
value of a: 10
value of b: 10.23
value of c: Hello
Type of a: int
Type of b: float64
Type of c: string