We can broadly classify the basic data types of the Go
language into the following categories.
Type | Description |
Boolean | Two predefined values, True and False. |
Numeric | Arithmetic type which includes integers and floating point. |
String | Simply represents the string values. (Immutable) |
Go
offers both implicit and explicit types. In the case of implicit type, the untyped constant is implicitly converted to a typed constant where required. For example: i := 0, same as var i int = 0.
It can only store one of the two values:
The following code shows how Boolean can be used in Go
.
package mainimport "fmt"func main() {string1 := "educative"string2 := "educative"string3 := "edpresso"result1 := string1 == string2result2 := string1 == string3// Shows the result and typefmt.Println("Value of result1 :", result1)fmt.Printf("Type of result1 : %T\n", result1)fmt.Println("Value of result2 :", result2)fmt.Printf("Type of result2 : %T\n", result2)}
Numeric types are used to represent different types of numbers. The following table shows the sub-categories of numeric types.
Type | Description | |
Integers | ||
uint8 | Unsigned 8-bit integers 0 to 255 | |
uint16 | Unsigned 16-bit integers 0 to 65535 | |
uint32 | Unsigned 32-bit integers 0 to 4294967295 | |
uint64 | Unsigned 64-bit integers 0 to 4294967295 | |
int8 | Signed 8-bit integers -128 to 127 | |
int16 | Signed 16-bit integers -32768 to 32767 | |
int32 | Signed 32-bit integers -2147483648 to 2147483647 | |
int64 | Signed 64-bit integers -9223372036854775808 to 9223372036854775807 | |
Floating Point | Approximate | |
float32 | (IEEE-754) floating-point 32-bit numbers | |
float64 | (IEEE-754) floating-point 64-bit numbers | |
complex64 | Complex numbers with float32 as a real and imaginary part. | |
complex128 | Complex numbers with float64 as a real and imaginary part. |
- Float value cannot be assigned to int data type. For example: var i int8 = 0.9877 will return an error.
- Int value can be stored in float data type. For example: var i float = 1 will not return an error.
The following code shows how different numeric data types can be used in Go
.
package mainimport "fmt"func main() {// 8-bit unsigned intvar A uint8 = 225fmt.Println("Value of A :", A)fmt.Printf("Type of A : %T\n", A)// 8-bit signed intvar B int8 = -112fmt.Println("Value of B :", B)fmt.Printf("Type of B : %T\n", B)// float32var C float32 = 0.6046603fmt.Println("Value of C :", C)fmt.Printf("Type of C : %T\n", C)// complex64var D complex64 = complex(10, 20)fmt.Println("Value of D :", D)fmt.Printf("Type of D : %T\n", D)}
This represents the string values. Strings are immutable, which means that once they are created, it is impossible to change their contents.
The following code shows how strings can be used in Go
.
package mainimport "fmt"func main() {string1 := "Educative"fmt.Println("Value of string is:", string1)fmt.Printf("Type of string is: %T", string1)}
Rune
is a superset of ASCII and represents Unicode codepoints. It is an alias for int32. For example, therune
literal (‘A’ )is actually the number 65. Below is an example ofrune
.
package mainimport "fmt"func main() {var runeVariable rune = 'A'fmt.Println("Value of runeVariable is:", runeVariable)fmt.Printf("Type of runeVariable is: %T", runeVariable)}