go create multiple variables on a single line
Certainly! Here is an example of creating multiple variables on a single line in the Go programming language, along with explanations for each step:
package main
import "fmt"
func main() {
var (
name string = "John"
age int = 30
isMale bool = true
salary float64 = 5000.50
language string = "Go"
)
fmt.Println("Name:", name)
fmt.Println("Age:", age)
fmt.Println("Is Male:", isMale)
fmt.Println("Salary:", salary)
fmt.Println("Language:", language)
}
Explanation:
var
: This keyword is used to declare variables in Go.name string = "John"
: This creates a string variable namedname
and assigns the value "John" to it. Thestring
type indicates that the variable can store text.age int = 30
: This creates an integer variable namedage
and assigns the value 30 to it. Theint
type indicates that the variable can store whole numbers.isMale bool = true
: This creates a boolean variable namedisMale
and assigns the valuetrue
to it. Thebool
type indicates that the variable can store eithertrue
orfalse
.salary float64 = 5000.50
: This creates a floating-point variable namedsalary
and assigns the value 5000.50 to it. Thefloat64
type indicates that the variable can store decimal numbers.language string = "Go"
: This creates a string variable namedlanguage
and assigns the value "Go" to it.fmt.Println("Name:", name)
: This prints the value of thename
variable to the console, along with the label "Name:".fmt.Println("Age:", age)
: This prints the value of theage
variable to the console, along with the label "Age:".fmt.Println("Is Male:", isMale)
: This prints the value of theisMale
variable to the console, along with the label "Is Male:".fmt.Println("Salary:", salary)
: This prints the value of thesalary
variable to the console, along with the label "Salary:".fmt.Println("Language:", language)
: This prints the value of thelanguage
variable to the console, along with the label "Language:".
I hope this explanation helps! Let me know if you have any further questions.