Back to: Golang
Data types are used to specify the type of data that a variable can hold. There are several built-in data types in Golang, including int
, float64
, bool
, string
, and rune
.
Here are some examples of how to use these data types:
int
The int
data type is used to store integer values. Here’s an example:
package main
import "fmt"
func main() {
age := 42
fmt.Println(age)
}
In this example, we declare a variable called age
and assign it a value of 42. We then use the fmt.Println
function to print the value of the age
variable to the console.
float64
The float64
data type is used to store floating-point values. Here’s an example:
package main
import "fmt"
func main() {
temperature := 72.5
fmt.Println(temperature)
}
In this example, we declare a variable called temperature
and assign it a value of 72.5. We then use the fmt.Println
function to print the value of the temperature
variable to the console.
bool
The bool
data type is used to store boolean values (true
or false
). Here’s an example:
package main
import "fmt"
func main() {
isRaining := true
fmt.Println(isRaining)
}
In this example, we declare a variable called isRaining
and assign it a value of true
. We then use the fmt.Println
function to print the value of the isRaining
variable to the console.
string
The string
data type is used to store string values. Here’s an example:
package main
import "fmt"
func main() {
message := "Hello, World!"
fmt.Println(message)
}
In this example, we declare a variable called message
and assign it a value of “Hello, World!”. We then use the fmt.Println
function to print the value of the message
variable to the console.
rune
The rune
data type is used to store Unicode code points. Here’s an example:
package main
import "fmt"
func main() {
letter := 'A'
fmt.Println(letter)
}
In this example, we declare a variable called letter
and assign it a value of ‘A’. We then use the fmt.Println
function to print the value of the letter
variable to the console.
Follow us on social media
Follow Author