English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
O (file operations)Data type specifies validGo variables
Data types that can be saved. In Go language, types are divided into the following four categories:Basic types:
Numbers, strings, and boolean values belong to this category.Aggregate types:
Arrays and structures belong to this category.Reference types:
Pointer, slice, map set, function, and Channel belong to this category.
Here, we will discuss the interface types of Go languageBasic data types. InBasic data typesare further divided into three subcategories, namely:
Numbers
Booleans
Strings
In Go language, numbers are divided intothreeSubcategory:
Integer:In Go language, both signed and unsigned integers can use four different sizes, as shown in the table below. The signed int is represented by int, and the unsigned integer is represented by uint.
Data type | Description |
---|---|
int8 | 8bit signed integer |
int16 | 16bit signed integer |
int32 | 32bit signed integer |
int64 | 64bit signed integer |
uint8 | 8bit unsigned integer |
uint16 | 16bit unsigned integer |
uint32 | 32bit unsigned integer |
uint64 | 64bit unsigned integer |
int | in and uint both contain the same size, whether it is32bit or64bit. |
uint | in and uint both contain the same size, whether it is32bit or64bit. |
rune | It is int32synonym, also indicating Unicode code points. |
byte | It is int8synonym. |
uintptr | It is an unsigned integer type. Its width is undefined, but it can accommodate all bits of a pointer value. |
// using integers package main import "fmt" func main() { // used8bit unsigned integer var X uint8 = 225 fmt.Println(X+1, X) // used16bit signed integer var Y int16 = 32767 fmt.Println(Y+2, Y-2) }
Output:
226 225 -32767 32765
Floating-point numbers:In Go language, floating-point numbers are divided into2As shown in the following table:
Data type | Description |
---|---|
float32 | 32bit IEEE 754Floating-point numbers |
float64 | 64bit IEEE 754Floating-point numbers |
// Usage of floating-point numbers package main import "fmt" func main() { a := 20.45 b := 34.89 //Subtraction of two floating-point numbers c := b-a //Display the result fmt.Printf("Result: %f", c) //Display the type of c variable fmt.Printf("\nThe type of c is : %T", c) }
Output:
Result: 14.440000 The type of c is: float64
Complex numbers:Divide complex numbers into two parts, as shown in the table below. float32and float64are also part of these complex numbers. Built-in functions create a complex number from its imaginary and real parts, and built-in imaginary and real functions extract these parts.
Data type | Description |
---|---|
complex64 | Containing float32As complex numbers consisting of real and imaginary components. |
complex128 | Containing float64As complex numbers consisting of real and imaginary components. |
//Use of complex numbers package main import "fmt" func main() { var a complex128 = complex(6, 2) var b complex64 = complex(9, 2) fmt.Println(a) fmt.Println(b) //Display type fmt.Printf("The type of a is %T as well as"+ "The type of b is %T", a, b) }
Output:
(6+2i) (9+2i) The type of a is complex128 and the type of b is complex64
The boolean data type only represents true or false. The values of the boolean type do not implicitly or explicitly convert to any other type.
//Use of boolean values package main import "fmt" func main() { //variable str1 := "w3codebox" str2 := "w3codebox" str3 := "w3codebox" result1 := str1 == str2 result2 := str1 == str3 //Print result fmt.Println(result1) fmt.Println(result2) //Display result1and result2The type fmt.Printf("result1 The type is %T , "+"result2The type is %T", result1, result2) }
Output:
true true result1 The type is bool , result2The type is bool
The string data type represents a sequence of Unicode code points. In other words, we can say that a string is an immutable byte sequence, which means that once a string is created, you cannot change the string. Strings can contain any data, including bytes that represent zero values in human-readable form.
//Using string package main import "fmt" func main() { //The str variable is used to store the string str := "w3codebox" //Display the length of the string fmt.Printf("The length of the string:%d", len(str)) //Display the string fmt.Printf("\nThe string is: %s", str) // Display the type of str variable fmt.Printf("\nThe type of str is: %T", str) }
Output:
The length of the string:5 The string is: w3codebox The type of str is: string