Data Types in GO
In this tutorial, we are going to discuss data types in the Go language. Go language is statically typed and has inbuilt data types.
Golang is a statically typed programming language meaning that each variable has a type.
Once a variable has a type set or inferred, it must not and cannot be changed. The following are the basic types available in Golang.
- Boolean
- Numeric Types
- int8, int16, int32, int64, int
- uint8, uint16, uint32, uint64, uint
- float32, float64
- complex64, complex128
- byte
- rune
- string
Boolean
The boolean data type can be one of two values, either true or false and is defined as bool when declaring it as a data type. The default value for boolean is false.
The values true and false will always be with a lowercase t and f, respectively, as they are pre-declared identifiers in Go.
E.g
package main
import "fmt"
func main() {
a := false
b := true
fmt.Println("a:", a, "b:", b)
c := a && b
fmt.Println("c:", c)
d := a || b
fmt.Println("d:", d)
}
Output
a: false b: true
c: false
d: true
In the program above, a is assigned to false, and b is assigned a true value. c is assigned the value of a && b. The && operator returns true only when both a and b are true. So, in this case, c is false.
The || operator returns true when either a or b is true. In this case, d is assigned to true since b is true.
Signed integers
int8: represents 8 bit signed integers
size: 8 bits
range: -128 to 127
int16: represents 16 bit signed integers
size: 16 bits
range: -32768 to 32767
int32: represents 32 bit signed integers
size: 32 bits
range: -2147483648 to 2147483647
int64: represents 64 bit signed integers
size: 64 bits
range: -9223372036854775808 to 9223372036854775807
int: represents 32 or 64 bit integers depending on the underlying platform. You should generally be using int to represent integers unless there is a need to use a specific sized integer.
size: 32 bits in 32 bit systems and 64 bit in 64 bit systems.
range: -2147483648 to 2147483647 in 32 bit systems and -9223372036854775808 to 9223372036854775807 in 64 bit systems
E.g
package main
import "fmt"
func main() {
var age int = 29
sal := 45000
fmt.Println("My age is",age, "and salary is",sal)
}
Output
My age is 29 and salary is 45000
In the above program, age is of type int, and the type of sal is inferred from its value (45000). As stated above, the int size is 32 bit in 32 bit systems and 64 bit in 64 bit systems. Let’s go ahead and verify this claim.
Verification
The type of variable can be printed using the %T format specifier in the Printf function.
Golang has a package unsafe, which has a Sizeof function that returns the size of the variable passed to it in bytes. unsafe package should be used with care as the code using it might have portability issues, but we can use it for this tutorial.
The following program outputs the type and size of both variables, age, and sal. %T is the format specifier to print the type, and %d is used to print the size.
package main
import (
"fmt"
"unsafe"
)
func main() {
var age int = 29
sal := 45000
fmt.Println("My age is",age, "and salary is",sal)
fmt.Printf("Type of age is %T, size of age is %d", age, unsafe.Sizeof(age))
fmt.Printf("\nType of sal is %T, size of sal is %d", sal, unsafe.Sizeof(sal))
}
Output
My age is 29 and salary is 45000
Type of age is int, size of age is 8
Type of sal is int, size of sal is 8
We can infer from the above output that age and sal are of type int, 64 bit sized (8 bytes). The output will vary if you run the above program on a 32-bit system. In a 32-bit system, a and b occupy 32-bits (4 bytes).
Unsigned integers
uint8: represents 8 bit unsigned integers
size: 8 bits
range: 0 to 255
uint16: represents 16 bit unsigned integers
size: 16 bits
range: 0 to 65535
uint32: represents 32 bit unsigned integers
size: 32 bits
range: 0 to 4294967295
uint64: represents 64 bit unsigned integers
size: 64 bits
range: 0 to 18446744073709551615
uint : represents 32 or 64 bit unsigned integers depending on the underlying platform.
size : 32 bits in 32 bit systems and 64 bits in 64 bit systems.
range : 0 to 4294967295 in 32 bit systems and 0 to 18446744073709551615 in 64 bit systems
Floating point types
float32: 32 bit floating point numbers
float64: 64 bit floating point numbers
The following is a simple program to illustrate integer and floating point types
E.g
package main
import (
"fmt"
)
func main() {
a, b := 10.72, 20.45
fmt.Printf("Type of a %T b %T\n", a, b)
sum := a + b
diff := a - b
fmt.Println("Sum", sum, "Difference", diff)
}
Output
Type of a float64 b float64
Sum 31.17 Difference -9.729999999999999
The type of a and b is inferred from the value assigned to them. In this case a and b are of type float64. (float64 is the default type for floating point values).
We add a and b and assign it to a variable sum. We subtract b from a and assign it to diff.
Complex types
complex64: complex numbers which have float32 real and imaginary parts
complex128: complex numbers with float64 real and imaginary parts
The builtin function complex is used to construct a complex number with real and imaginary parts. The complex function has the following definition
<code>func complex(r, i FloatType) ComplexType </code>
It takes a real and imaginary part as a parameter and returns a complex type. Both the real and imaginary parts must be of the same type. ie either float32 or float64.
If both the real and imaginary parts are float32, this function returns a complex value of type complex64. If both the real and imaginary parts are of type float64, this function returns a complex value of type complex128.
Complex numbers can also be created using the shorthand syntax
<code>c := 6 + 7i</code>
E.g
package main
import (
"fmt"
)
func main() {
c1 := complex(10, 15)
c2 := 20 + 25i
cadd := c1 + c2
fmt.Println("Sum is:", cadd)
cmul := c1 * c2
fmt.Println("Product is:", cmul)
}
Output
Sum is: (30+40i)
Product is: (-175+550i)
Other numeric types
byte is an alias of uint8
rune is an alias of int32
string type
A string is a sequence of one or more characters (letters, numbers, symbols) that can be either a constant or a variable.
Strings exist within either back quotes `
or double quotes "
in Go and have different characteristics depending on which quotes you use.
If you use the back quotes, you are creating a raw string literal. If you use the double quotes, you are creating an interpreted string literal.
Raw String Literals
Raw string literals are character sequences between back quotes, often called back ticks. Within the quotes, any character will appear just as it is displayed between the back quotes, except for the back quote character itself.
E.g
package main
import (
"fmt"
)
func main() {
message := `<code>Welcome to "Waytoeasylearn" website</code>`
fmt.Println(message)
}
Output
Welcome to "Waytoeasylearn" website
Usually, backslashes are used to represent special characters in strings. For example, in an interpreted string, \n
would represent a new line in a string. However, backslashes have no special meaning inside of raw string literals
package main
import (
"fmt"
)
func main() {
message := `<code>Welcome to "Waytoeasylearn" website</code> \n`
fmt.Println(message)
}
Output
Welcome to "Waytoeasylearn" website \n
Because the backslash has no special meaning in a string literal, it will actually print out the value of \n
instead of making a new line.
Raw string literals may also be used to create multi line strings
E.g
package main
import (
"fmt"
)
func main() {
a := `<code>This string is on
multiple lines within a
single back quote on either side.</code>`
fmt.Println(a)
}
Output
This string is on
multiple lines within a
single back quote on either side.
Interpreted String Literals
Interpreted string literals are character sequences between double quotes, as in "Waytoeasylearn"
. Within the quotes, any character may appear except newline and unescaped double quotes.
To show double quotes in an interpreted string, you can use the backslash as an escape character, like so
package main
import (
"fmt"
)
func main() {
message := "Welcome to \"Waytoeasylearn\" website"
fmt.Println(message)
}
Output
Welcome to "Waytoeasylearn" website
You will almost always use interpreted string literals because they allow for escape characters within them.