Go language data type
GO language learning tutorial www.lsdcloud.com
The Go language has the following data types by category:
The data type | describe |
---|---|
The Boolean | Boolean values can only be constants true or false. A simple example: var b bool = true |
Numeric types | Int and floating-point float32, float64, Go language support integer and floating-point numbers, and native support for complex numbers, where the bit operation using complement code. |
String type | A string is a sequence of characters of fixed length joined together. The Go string is concatenated by a single byte. The bytes of strings in the Go language use utF-8 encoding to identify Unicode text. |
The derived type | (a) Pointer (b) Array type (c) Struct (D) Channel type (e) Function type (f) Slice type (G) Interface type (h) Map type |
Go has some default behavior for case:
- Variables that begin with a capital letter are, are public variables; Lowercase letters are private variables.
- The same is true for functions that begin with a capital letter, which is equivalent to Java’s public functions with the public keyword;
- A private function that begins with a lowercase letter and corresponds to the private keyword.
Number type:
Go also has architecture-based types such as int, uint and Uintptr.
The serial number | Type and Description |
---|---|
1 | uint8Unsigned 8-bit integer (0 to 255) |
2 | uint16Unsigned 16-bit integer (0 to 65535) |
3 | uint32Unsigned 32-bit integer (0 to 4294967295) |
4 | uint64Unsigned 64-bit integer (0 to 18446744073709551615) |
5 | int8Signed 8-bit integers (-128 to 127) |
6 | int16Signed 16-bit integers (-32768 to 32767) |
7 | int32Signed 32-bit integers (-2147483648 to 2147483647) |
8 | int64Signed 64-bit integers (-9223372036854775808 to 9223372036854775807) |
Floating point:
The serial number | Type and Description |
---|---|
1 | float32Ieee-754 32-bit floating point number |
2 | float64Ieee-754 64-bit floating point number |
3 | complex6432-bit real and imaginary numbers |
4 | complex12864-bit real and imaginary numbers |
Other numeric types
The serial number | Type and Description |
---|---|
1 | byteSimilar uint8 |
2 | runeSimilar int32 |
3 | uint32 or 64 bits |
4 | intSame size as uint |
5 | uintptrAn unsigned integer used to hold a pointer |
Compound type (derived type)
The serial number | Type and Description |
---|---|
1 | Pointer type |
2 | An array type |
3 | Structured type (struct) |
4 | The Channel type |
5 | Function types |
6 | Slice type |
7 | Interface Type |
8 | The Map type |
For example, the following
The integer
After version 1.13, the number literal syntax was introduced to make it easier for developers to define numbers in binary, octal, or hexadecimal floating-point formats, such as V := 0b00101101, which represents 101101 in binary and 45 in decimal. V := 0o377, which represents 377 in octal and 255 in decimal. V := 0x1p-2, which is the hexadecimal 1 divided by 2², which is 0.25. It also allows us to separate numbers with _, for example: v := 123_456 equals 123456.
package main
import "fmt"
func main(a){
/ / decimal
var a int = 10
fmt.Printf("%d \n", a) / / 10
fmt.Printf("%b \n", a) // the 1010 placeholder %b represents binary
// octal starts with 0
var b int = 077
fmt.Printf("%o \n", b) / / 77
// Hexadecimal starts with 0x
var c int = 0xff
fmt.Printf("%x \n", c) // ff
fmt.Printf("%X \n", c) // FF
}
Copy the code
floating-point
Go supports two floating-point numbers: float32 and float64. These two floating-point data formats follow the IEEE 754 standard: floating-point numbers in float32 have a maximum range of about 3.4e38 and can be defined using constants: math.maxFloat32. The maximum range of floating-point numbers in float64 is about 1.8e308 and can be defined using a constant: math.maxFloat64. When printing floating point numbers, you can use the FMT package with the verb %f as follows
package main
import (
"fmt"
"math"
)
func main(a) {
fmt.Printf("%f\n", math.Pi)
fmt.Printf("%.2f\n", math.Pi)
}
Copy the code
Boolean value
In Go, Boolean data is declared as bool. Boolean data has only true (true) and false (false)
Note:
- The default value for a Boolean variable is false.
- Casting integers to Booleans is not allowed in Go.
- Booleans cannot participate in numeric operations and cannot be converted to other types.
string
Strings in the Go language appear as native data types and are used just like any other native data type (int, bool, FLOAT32, float64, etc.). The internal implementation of strings in Go uses UTF-8 encoding. String values are in double quotation marks (“). You can add non-ASCII characters directly to the Go source code, for example:
s1 := "hello"
s2 := "Hello"
Copy the code
String escape character
Common escape characters in the Go language include carriage return, line feed, single and double quotation marks, and tabs, as shown in the following table.
Escape character | Escape character |
---|---|
\r | Carriage return (returns the beginning of the line) |
\n | Line break (jumps directly to the same column on the next line) |
\t | tabs |
‘ | Single quotes |
“ | Double quotation marks |
\ | The backslash |
Multiline string
To define a multi-line string in Go, we must use the backquotation ‘character:
s1 := 'First line, second line, third line
fmt.Println(s1)
Copy the code
A newline between backquotes is treated as a newline in the string, but all escape characters are invalid, and the text is printed as is.
Common operations on strings
methods | methods |
---|---|
len(str) | For the length of the |
+ or FMT. Sprintf | Concatenated string |
strings.Split | segmentation |
strings.contains | Determine whether it contains |
strings.HasPrefix,strings.HasSuffix | Prefix/suffix judgment |
strings.Index(),strings.LastIndex() | The position where the substring appears |
strings.Join(a[]string, sep string) | Join operation |
Byte and rune
The elements that make up each string are called “characters,” which can be obtained by traversing or retrieving string elements individually. Characters are enclosed in single quotes (‘), as in:
var a := 'in'
var b := 'x'
Copy the code
There are two types of characters in the Go language:
- The Uint8 type, or byte, represents a character in the ASCII code.
- The rune type represents a UTF-8 character.
When you need to handle Chinese, Japanese, or other compound characters, you need to use the rune type. The rune type is actually an INT32.
Go uses a special rune type to handle Unicode, which facilitates Unicode-based text processing, as well as a byte type for default string processing, with performance and scalability benefits.
// Iterate over the string
func traversalString(a) {
s := Shahe, "hello"
for i := 0; i < len(s); i++ { //byte
fmt.Printf("%v(%c) ", s[i], s[i])
}
fmt.Println()
for _, r := range s { //rune
fmt.Printf("%v(%c) ", r, r)
}
fmt.Println()
}
Copy the code
The output
104(h) 101(e) 108(l) 108(l) 111(o) 230(æ) 178(squared)153(a)230(æ) 178(squared)179(after)104(h) 101(e) 108(l) 108(l) 111(o) 27801(sand)27827(river)Copy the code
Since UTF8 encodes a Chinese character in three to four bytes, we cannot simply iterate through a string containing Chinese characters by byte, otherwise we will see the result of the first line in the output above.
At the bottom of the string is an array of bytes, which can be converted to and from [] bytes. A string cannot be modified. A string consists of byte bytes. Therefore, the length of a string is the length of byte bytes. The rune type is used to represent utF8 characters. A rune character consists of one or more bytes.
Modify string
To modify a string, first convert it to []rune or []byte, and then convert it to string. In either case, memory is reallocated and the byte array is copied.
func changeString(a) {
s1 := "big"
// Cast
byteS1 := []byte(s1)
byteS1[0] = 'p'
fmt.Println(string(byteS1))
s2 := "White radish"
runeS2 := []rune(s2)
runeS2[0] = 'red'
fmt.Println(string(runeS2))
}
Copy the code
Type conversion
The Go language has only cast, not implicit cast. This syntax can only be used when conversions between two types are supported.
The basic syntax for casts is as follows:
T(expression)
Where T represents the type to be converted. Expressions include variables, complex operators and function return values.
For example, the Sqrt() function of the Math package is used to calculate the hypotenuse of a right triangle. The Sqrt() function takes in float64, and the variables A and B are int. In this case, you need to cast a and B to float64.
func sqrtDemo(a) {
var a, b = 3.4
var c int
Math.sqrt () receives arguments of type FLOAT64 that need to be cast
c = int(math.Sqrt(float64(a*a + b*b)))
fmt.Println(c)
}
Copy the code
Practice writing code to count the number of Chinese characters in the string “Hello China Hello World”.
package main
import (
"fmt"
"unicode"
)
func isHan(r rune) bool {
return unicode.Is(unicode.Han, r)
}
func main(a) {
words:="Hello China, Hello World"
var data []rune = []rune(words)
sum:=0
fmt.Println(len(data))
for i:=0; i<len(data) ; i++ {
b := data[i]
if isHan(b) {
sum++
}
}
fmt.Printf("%s contains % D Characters",words,sum)
}
Copy the code
GO language learning tutorials www.lsdcloud.com