Go Basic Types
Basic Types
Type | Bytes | Default | Description | |
bool | 1 | false | Boolean values only have two values: true and false | |
byte | 1 | 0 | uint8 | |
rune | 4 | 0 | int32,often used for processing Unicode | |
Integer | int,uint | 4 or 8 | 0 | For 32-bit operating systems, it's 4; for 64-bit operating systems, it's 8 |
int8,uint8 | 1 | 0 | ||
int16,uint16 | 2 | 0 | ||
int32,uint32 | 4 | 0 | ||
int64,uint64 | 8 | 0 | ||
Float | float32 | 4 | 0.0 | |
float64 | 8 | 0.0 | ||
Complex | complex64 | 8 | ||
complex128 | 16 | |||
uintptr | 4 or 8 | uint32 or uint64,used to store memory addresses | ||
array | ||||
struct | The zero value of the corresponding type for each field in a struct | |||
string | “” | Encoded in UTF-8 and enclosed in double quotes | ||
slice | nil | Reference Types | ||
map | nil | Reference Types | ||
channel | nil | Reference Types | ||
interface | nil | |||
function | nil |
Multiline Strings (Backticks)
In Go, to define a multiline string, you must use backtick characters:
s1 := `1st line
2nd line
3rd line
`
Common String Operations
Functions | Description |
len(str) | Get the length of a string |
+ or fmt.Sprintf | Combine two or more strings |
strings.Split | Divide a string into substrings based on a delimiter |
strings.Contains | Check if a string contains a specific substring |
strings.HasPrefix,strings.HasSuffix | Verify if a string starts or ends with a certain sequence |
strings.Index(),strings.LastIndex() | Find the position of a substring within a string |
strings.Join | Combine multiple strings into a single string using a separator |
Type Conversion
Go only supports explicit type conversion, not implicit. It can only be used when the two types involved support mutual conversion. The basic syntax for explicit type conversion is as follows:
T(expression)
Where T represents the type to convert to, and expression can include variables, function return values, etc.
Take a break
👉👉👉 【Jade Dynasty EP50】Gui Li and Xiao Bai assisted the Jin Clan in resisting the invasion