In Go, basic data types are called values and can be assigned to variables. Go has several basic data types including string, integer, floating point, boolean, and character. In this tutorial, we’ll go over the basic data types in Go and see how to work with them.
Strings
A string is a sequence of characters surrounded by double quotes. In Go, strings are immutable, which means that once a string is created, it cannot be modified. Here’s an example:
package main
import "fmt"
func main() {
name := "John Doe"
fmt.Println("Hello", name)
}
Integers
Integers are whole numbers and can be positive, negative, or zero. Go has several integer types, including int8, int16, int32, and int64. By default, Go uses int
, which is a platform-dependent integer type. Here’s an example:
package main
import "fmt"
func main() {
age := 25
fmt.Println("Age:", age)
}
Floating Point Numbers
Floating point numbers are numbers with a decimal point and can be positive, negative, or zero. Go has two floating point types, float32 and float64. By default, Go uses float64
. Here’s an example:
package main
import "fmt"
func main() {
height := 1.80
fmt.Println("Height:", height)
}
Booleans
Booleans are binary values that can be either true
or false
. Here’s an example:
package main
import "fmt"
func main() {
isProgrammer := true
fmt.Println("Is a programmer:", isProgrammer)
}
Characters
Go has a character type rune
that represents a Unicode code point. In Go, characters are written as single quotes. Here’s an example:
package main import "fmt" func main() { letter := 'A' fmt.Println("Letter:", string(letter)) }
In this tutorial, we’ve covered the basic data types in Go. You can use these types to create variables and perform operations with them. In the next tutorial, we’ll go over the composite data types in Go.
Leave a Reply