Hello!
In this programming, we will look at the basic data types in golang.
Bool
The first type we will consider is bool. In this type, only two values are possible: true and false. You can create a bool variable in one of the following ways:
var isActive bool
var isEnabled = false
isLoaded := true
In the second and third options, we immediately set the value that the variable will have. In the first, we do not specify anything, so a zero value will be used. For the bool type, it will be false. I will display the type and value of each variable
package main
import "fmt"
func main() {
var isActive bool
var isEnabled bool = false
isLoaded := true
fmt.Printf("%v: %T\n", isActive, isActive)
fmt.Printf("%v: %T\n", isEnabled, isEnabled)
fmt.Printf("%v: %T\n", isLoaded, isLoaded)
}
$ go run main.go
false: bool
false: bool
true: bool
Int
The next one is int. There are several int types in golang, and there is also unsigned int. Signed int can include numbers with a minus sign, unsigned only positive numbers. Accordingly, all int types in golang are uint8, uint16, uint32, uint64, int8, int16, int32, and int64.
. They differ in what range of numbers we can use. For example, for int8 this range will be -128 to 127
and for uint, it will be 0 to 255
and with each subsequent type, this range will increase. variables are created in the same way as the bool type
var a int64
var b int
var l uint = 20
var c = 35
d := 86
Now I will output each value and type
package main
import "fmt"
func main() {
var a int64
var b int
var l uint = 20
var c = 35
d := 86
fmt.Printf("%v: %T\n", a, a)
fmt.Printf("%v: %T\n", b, b)
fmt.Printf("%v: %T\n", l, l)
fmt.Printf("%v: %T\n", c, c)
fmt.Printf("%v: %T\n", d, d)
}
go run main.go
0: int64
0: int
20: uint
35: int
86: int
For int, the null value will be 0.
Mathematical operations can be performed on the int type. But it must be the same int type. If we have int64 and int32 we will not be able to perform mathematical operations. In this case, you will first need to bring them to the same type.
The following mathematical operations are available: addition(+), subtraction(-), multiplication(*), division(/), remainder(%). Most operations are self-explanatory. Only division and remainder require additional attention. Since there are no decimal places in int. After dividing 10 by 3, we will get the whole number 3. and the remainder remains 1. To get the remainder exactly, the % operation is used
package main
import "fmt"
func main() {
var a int = 10
var b int = 3
fmt.Println(a + b)
fmt.Println(a - b)
fmt.Println(a * b)
fmt.Println(a / b)
fmt.Println(a % b)
}
$ go run main.go
13
7
30
3
1
Float
Comma numbers are created similarly to ints and have the same math operations except for calculating the remainder(%).
package main
import "fmt"
func main() {
var a float32 = 10
var b float32 = 3
c := 10.4
var d = 10.2
fmt.Printf("%f: %T\n", a, a)
fmt.Printf("%f: %T\n", b, b)
fmt.Printf("%f: %T\n", c, c)
fmt.Printf("%f: %T\n", d, d)
}
$ go run main.go
10.000000: float32
3.000000: float32
10.400000: float64
10.200000: float64
And mathematical operations
package main
import "fmt"
func main() {
var a float32 = 10.2
var b float32 = 3.4
fmt.Println(a + b)
fmt.Println(a - b)
fmt.Println(a * b)
fmt.Println(a / b)
}
$ go run main.go
13.6
6.7999997
34.68
2.9999998
String
The last type for today is a string.
package main
import "fmt"
func main() {
var a string = "Hello World"
var c = "Hello"
b := "World"
fmt.Printf("%s: %T\n", a, a)
fmt.Printf("%s: %T\n", b, b)
fmt.Printf("%s: %T\n", c, c)
}
$ go run main.go
Hello World: string
World: string
Hello: string
There is one mathematical operation for string, this is +. It allows you to add two string variables.
package main
import "fmt"
func main() {
var c = "Hello "
b := "World!"
fmt.Printf(c + b)
}
$ go run main.go
Hello World!
You can also access the string type using indexes. You can imagine that the variable of the type string is a list, in golang, the numbering in the list starts from zero. In this way, you can get only part of the string from it. Example
package main
import "fmt"
func main() {
var c = "Hello "
fmt.Printf("%v", string(c[2]))
}
$ go run main.go
l
This way I get the third character from the variable named c. But besides that, you still need to convert the received value to the string type. Otherwise, I would get the number 108, which in UTF stands for l.
Unlike lists, I cannot replace a character in a string. Because the string is an immutable data type. But I can convert a string to a list of bytes
import "fmt"
func main() {
var c = "Hello "
b := []byte(c)
fmt.Println(b)
}
$ go run main.go
[72 101 108 108 111 32]
And then I can replace any value in the string
package main
import "fmt"
func main() {
var c = "Hello "
b := []byte(c)
b[0] = byte(122)
fmt.Println(string(b))
}
$ go run main.go
zello
But initial string c will stay the same. Only array b will be changed