Home »
Golang
Shorthand to Define Multiple Variables/Constants in Golang
In this tutorial, we are going to learn how to use shorthand to define multiple variables/constants in Go Language?
Submitted by IncludeHelp, on March 25, 2021
In Go programming language, there is another shorthand to define multiple variables/constants together.
Variables are declared using the var keyword and constants are declared using the const keyword.
Both multiple variables & constants can be defined using the following syntax,
Syntax to define multiple variables:
var (
name = value
name = value
name = value
...
)
Syntax to define multiple constants:
const (
name = value
name = value
name = value
...
)
Practice the following programs illustrating the shorthand defining 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.
1) Defining multiple variables of same type – Example
// Golang program for defining 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
2) Defining multiple variables of mixed type – Example
// Golang program for defining 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
3) Defining multiple constants of same type – Example
// Golang program for defining 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
4) Defining multiple constants of mixed type – Example
// Golang program for defining 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