Basic Data Types [Numeric, Character, Boolean, String]#
Numeric Types [Integer Types, Floating-Point Types]#
Integer Types [Signed Integer Types, Unsigned Integer Types, Other Integer Types]#
==Types that store integer values, with a default value of 0==
【1】Signed Integer Types:
Type | Signed | Storage Size | Value Range |
---|---|---|---|
int8 | Yes | 1 byte | -27~27-1 (-128~127) |
int16 | Yes | 2 bytes | -215~215-1 (-32768~32767) |
int32 | Yes | 4 bytes | -231~231-1 (-2147483648~2147483647) |
int64 | Yes | 8 bytes | -263~263-1 |
【2】Unsigned Integer Types:
Type | Signed | Storage Size | Value Range |
---|---|---|---|
uint8 | No | 1 byte | 0~255 |
uint16 | No | 2 bytes | 0~216-1 |
uint32 | No | 4 bytes | 0~231-1 |
uint64 | No | 8 bytes | 0~263-1 |
【3】Other Integer Types:
Type | Signed | Storage Size | Value Range |
---|---|---|---|
int | Yes | 4 bytes on 32-bit systems 8 bytes on 64-bit systems | -231~231-1 -263~263-1 |
uint | No | 4 bytes on 32-bit systems 8 bytes on 64-bit systems | 0~232-1 0~264-1 |
rune | Yes | Equivalent to int32 | -231~231-1 |
byte | No | Equivalent to uint8 | 0~255 |
Floating-Point Types#
==Types that store decimal values, with a default value of 0==
Type | Storage Size | Value Range |
---|---|---|
float32 | 4 bytes | -3.403E38~3.403E38 |
float64 | 8 bytes | -1.798E308~1.798E308 |
The default floating-point type in Golang is float64
Character Types#
Golang does not have a specific character type. To store a single character, byte is generally used
ASCII Table:
Chinese characters correspond to Unicode code points, byte types will overflow, so int can be used
UTF-8 Encoding Table:
==Summary: Golang characters correspond to UTF-8 encoding (Unicode corresponds to the character set, UTF-8 is one of the encoding schemes)==
Escape Characters:
Boolean Type
==A type that can only take the values true and false, with a default value of false==
Boolean type, also known as bool type, occupies 1 byte and is suitable for logical operations, generally used for program flow control
String Type#
==A sequence of fixed-length characters connected together, with a default value of ""==
`1. If the string does not contain special characters, it is represented by double quotes ""; if the string contains special characters, it is represented by backticks ```
package main
import "fmt"
func main() {
var str1 string = "abc"
fmt.Println(str1)
var str2 string = `a\b\c`
fmt.Println(str2)
}
2. String concatenation and precautions:
package main
import "fmt"
func main() {
var str1 string = "abc" + "def"
str1 += "xyz"
fmt.Println(str1)
//If the string concatenation is too long and you want to wrap it, each line must end with a plus sign
var str2 string = "abc" + "abc" + "abc" +
"abc" + "abc" + "abc" +
"abc" + "abc" + "abc" +
"abc" + "abc" + "abc" +
"abc" + "abc" + "abc"
fmt.Println(str2)
}
Conversion Between Basic Data Types#
In Go, assignment between different types of variables requires explicit conversion, and only explicit conversion (forced conversion) is allowed
package main
import "fmt"
func main() {
var n1 int = 100
//var n2 float32 = n1 This cannot be converted, explicit conversion is required
var n2 float32 = float32(n1)
fmt.Println(n1)
fmt.Println(n2)
//When int64 is converted to int8, the compilation will not fail, but the data will overflow, similar to putting a large object into a small container
var n3 int64 = 888888
var n4 int8 = int8(n3)
fmt.Println(n4) //Output 56
}
Conversion Between Basic Data Types and string#
==During development, we often need to convert basic data types to string, or convert string to basic data types==
Method 1#
fmt.Sprintf("%parameter", expression), the parameter and the type of the expression must match
package main
import "fmt"
func main() {
var n1 int = 15
var n2 float32 = 5.5
var n3 bool = true
var n4 byte = 'A'
var m1 string = fmt.Sprintf("%d", n1)
fmt.Printf("m1's data type is: %T, %q", m1, m1)
fmt.Println()
var m2 string = fmt.Sprintf("%f", n2)
fmt.Printf("m2's data type is: %T,%q", m2, m2)
fmt.Println()
var m3 string = fmt.Sprintf("%t", n3)
fmt.Printf("m3's data type is: %T,%q", m3, m3)
fmt.Println()
var m4 string = fmt.Sprintf("%c", n4)
fmt.Printf("m4's data type is: %T,%q", m4, m4)
}
Output:
m1's data type is: string, "15"
m2's data type is: string,"5.500000"
m3's data type is: string,"true"
m4's data type is: string,"A"
Method 2#
Use functions in the strconv package
package main
import (
"fmt"
"strconv"
)
func main() {
var n1 int = 15
//The first parameter must be converted to int64, and the second parameter specifies the base
var m1 string = strconv.FormatInt(int64(n1), 10)
fmt.Printf("m1's data type is: %T, %q", m1, m1)
fmt.Println()
var n2 float64 = 5.5
//The second parameter 'f' (-ddd.dddd), the third parameter specifies the number of decimal places, and the fourth parameter specifies the floating-point type
var m2 string = strconv.FormatFloat(n2, 'f', 9, 64)
fmt.Printf("m2's data type is: %T,%q", m2, m2)
fmt.Println()
var n3 bool = true
var m3 string = strconv.FormatBool(n3)
fmt.Printf("m3's data type is: %T,%q", m3, m3)
fmt.Println()
}
Output:
m1's data type is: string, "15"
m2's data type is: string,"5.500000000"
m3's data type is: string,"true"
==Summary: Focus on Method 1==
Conversion from string to Basic Data Types#
Use functions in the strconv package
package main
import (
"fmt"
"strconv"
)
func main() {
//string---->bool
var n1 string = "true"
var m1 bool
//The ParseBool function returns two values (value bool, err error)
//value is the obtained boolean value, err is the error that occurred, err can be ignored directly with _
m1, _ = strconv.ParseBool(n1)
fmt.Printf("m1's data type is: %T, %v", m1, m1)
fmt.Println()
//string---->int64
var n2 string = "55"
var m2 int64
//The second parameter specifies the base, and the third parameter specifies the integer type that the result must be able to assign without overflow. 0, 8, 16, 32, 64 respectively represent int, int8, int16, int32, int64
m2, _ = strconv.ParseInt(n2, 10, 64)
fmt.Printf("m2's data type is: %T, %v", m2, m2)
fmt.Println()
//string---->float
var n3 string = "8.8"
var m3 float64
//The second parameter specifies the expected receiving type, 32 is float32 (the return value can be assigned to float32 without changing the precise value), 64 is float64
m3, _ = strconv.ParseFloat(n3, 64)
fmt.Printf("m3's data type is: %T, %v", m3, m3)
fmt.Println()
//Note: Make sure that the string type can be converted into valid data, for example, we can convert "123" into an integer, but we cannot convert "Hello" into an integer, otherwise it will become the default value of the converted type
var n4 string = "golang"
var m4 bool
m4, _ = strconv.ParseBool(n4)
fmt.Printf("m4's data type is: %T, %v", m4, m4)
fmt.Println()
var n5 string = "golang"
var m5 int64
m5, _ = strconv.ParseInt(n5, 10, 64)
fmt.Printf("m5's data type is: %T, %v", m5, m5)
}
Output:
m1's data type is: bool, true
m2's data type is: int64, 55
m3's data type is: float64, 8.8
m4's data type is: bool, false
m5's data type is: int64, 0