Basic data types
In the go language, data types are used to declare functions and variables.
Data types appear to divide data into data of different memory sizes. When programming, large data is needed to apply for large memory. When small data is needed, small memory is needed to make full use of space.
The go language has the following data by category:
type | describe |
---|---|
Boolean type | Boolean values can only be constant true or false. A simple example: var flag=true |
Number type | Number types include integer int, floating-point float32, and floating-point float64; Complex numbers are also supported. (Note: 0 or non-0 cannot be used to represent conditional judgment) |
String type | A string is a sequence of characters connected by a fixed length of characters. The go language strings are connected by a single byte, and the go language strings use utf8 encoding to identify unicode text. |
Derived Types | Derived types include pointer type, array type, slice type, struct type, channel type, interface type, map type, and so on. |
Integer type
The go language also has architecture-based types, such as int, uint, and uintptr.
type | describe |
---|---|
uint8 | Unsigned 8-bit integers (0 to 255) |
uint16 | Unsigned 16-bit integers (0 to 65535) |
uint32 | Unsigned 32-bit integer (0 to 429467295) |
uint64 | Unsigned 64-bit integer (0 to 18446744073709551616) |
int8 | Signed 8-bit integers (-128 to 127) |
int16 | Signed 16-bit integers (-32768 to 32767) |
int32 | Signed 32-bit integer (-2147483648 to 2147483647) |
int64 | Signed 64-bit integer (-9223372036854775808 to 9223372036854775807) |
package main import ( "fmt" "math" "unsafe" ) func main() { var a uint8 var b uint16 var c uint32 var d uint64 fmt.Printf("a: %v %T %dB %v~%v\n", a, a, unsafe.Sizeof(a), 0, math.MaxUint8) fmt.Printf("b: %v %T %dB %v~%v\n", b, b, unsafe.Sizeof(b), 0, math.MaxUint16) fmt.Printf("c: %v %T %dB %v~%v\n", c, c, unsafe.Sizeof(c), 0, math.MaxUint32) fmt.Printf("d: %v %T %dB %v~%v\n", d, d, unsafe.Sizeof(d), 0, math.MaxInt64) var e int8 var f int16 var g int32 var h int64 fmt.Printf("e: %v %T %dB %v~%v\n", e, e, unsafe.Sizeof(e), math.MinInt8, math.MaxInt8) fmt.Printf("f: %v %T %dB %v~%v\n", f, f, unsafe.Sizeof(f), math.MinInt16, math.MaxInt16) fmt.Printf("g: %v %T %dB %v~%v\n", g, g, unsafe.Sizeof(g), math.MinInt32, math.MaxInt32) fmt.Printf("h: %v %T %dB %v~%v\n", h, h, unsafe.Sizeof(h), math.MinInt64, math.MaxInt64) var i float32 var j float64 fmt.Printf("i: %v %T %dB %v~%v\n", i, i, unsafe.Sizeof(i), -math.MaxFloat32, math.MaxFloat32) fmt.Printf("j: %v %T %dB %v~%v\n", j, j, unsafe.Sizeof(j), -math.MaxFloat64, math.MaxFloat64) var k complex64 var l complex128 fmt.Printf("k: %v %T %dB \n", k, k, unsafe.Sizeof(k)) fmt.Printf("l: %v %T %dB \n", l, l, unsafe.Sizeof(l)) } /* a: 0 uint8 1B 0~255 b: 0 uint16 2B 0~65535 c: 0 uint32 4B 0~4294967295 d: 0 uint64 8B 0~9223372036854775807 e: 0 int8 1B -128~127 f: 0 int16 2B -32768~32767 g: 0 int32 4B -2147483648~2147483647 h: 0 int64 8B -9223372036854775808~9223372036854775807 i: 0 float32 4B -3.4028234663852886e+38~3.4028234663852886e+38 j: 0 float64 8B -1.7976931348623157e+308~1.7976931348623157e+308 k: (0+0i) complex64 8B l: (0+0i) complex128 16B */
Binary Interconversion
package main import "fmt" func main() { //Decimal number a := 10 fmt.Printf("a Decimal representation of a: %d\n", a) fmt.Printf("a Binary representation a: %b\n", a) fmt.Printf("a Octal representation of a: %o\n", a) fmt.Printf("a Hexadecimal representation of a: %x\n", a) //Define an octal number starting with 0 b := 077 fmt.Printf("b Decimal representation of a: %d\n", b) fmt.Printf("b Binary representation a: %b\n", b) fmt.Printf("b Octal representation of a: %o\n", b) fmt.Printf("b Hexadecimal representation of a: %x\n", b) //Define a hexadecimal number starting with 0x c := 0x111 fmt.Printf("c Decimal representation of a: %d\n", c) fmt.Printf("c Binary representation a: %b\n", c) fmt.Printf("c Octal representation of a: %o\n", c) fmt.Printf("c Hexadecimal representation of a: %x\n", c) }
Floating Point Type
type | describe |
---|---|
float32 | IEEE-754 32-bit floating point number |
float64 | IEEE-754 64-bit floating point number |
complex64 | 32-bit real and imaginary numbers |
complex128 | 64-bit real and imaginary numbers |
Other Number Types
type | describe |
---|---|
byte | Similar to uint8 |
rune | Similar to int32 |
String type
In the go language, strings are created with double quotes''or back quotes. Double quotation marks are used to create resolvable strings that support escape, but cannot be used to reference multiple lines. Inverse quotation marks are used to create native strings and can consist of multiple lines, but escape is not supported and can contain all characters except inverted quotation marks. Double quotation marks are most commonly used to create parsable strings, while reverse quotation marks are used to create native strings for writing multiple lines of messages, HTML, and regular expressions.
String Stitching
package main import ( "bytes" "fmt" "strings" ) func main() { //Single-line string supports escape a := "hello world" //Escape is not supported for multi-line strings b := ` <div> <p>hello</p> </div> ` fmt.Printf("a: %v\n", a) fmt.Printf("b: %v\n", b) //String Stitching s1 := "hello" s2 := "world" //1. Plus Stitching res1 := s1 + " " + s2 fmt.Printf("s: %v\n", res1) //2. String Formatting Sprintf res2 := fmt.Sprintf("%s %s", s1, s2) fmt.Printf("res2: %v\n", res2) //3.strings.join() /* join It calculates the length of a stitched string based on an array of strings, then requests memory of the corresponding size to fill in each character. This is efficient if you already have an array, but it's expensive to construct if you don't already have one. */ res3 := strings.Join([]string{s1, s2}, " ") //The first parameter is an array of strings, and the second parameter is a connector fmt.Printf("res3: %v\n", res3) //4.buffer.WriteString() /* This is ideal for variable strings, for optimizing memory growth, and for predicting string length, for setting capacity using the buffer.Grow() interface This is also written directly to the buffer, so it is more efficient */ var buffer bytes.Buffer buffer.WriteString(s1) buffer.WriteString(s2) fmt.Printf("buffer.String(): %v\n", buffer.String()) }
Escape Character
go language strings Common escape characters include carriage return, line break, single and double quotation marks, tabs, and so on
escape character | Meaning |
---|---|
\r | Carriage Return Symbol |
\n | Line Break |
\t | Tab (tab key, or four spaces) |
\' | Single quotation mark |
\" | Double Quotes |
\\ | Backslash |
Slicing operation
//Slicing operation s := "I am a student." m, n := 2, 4 fmt.Printf("s[m:n]: %v\n", s[m:n]) //Gets the value of the string s index position from m to n-1 fmt.Printf("s[:n]: %v\n", s[:n]) //Gets the value of the string s index position from 0 to n-1 fmt.Printf("s[m:]: %v\n", s[m:]) //Gets the value of the string s index position from m to len(s)-1 fmt.Printf("s[:]: %v\n", s[:]) //Get string s fmt.Printf("s[m]: %v\n", s[m]) //Gets the ascii value of the character at the position m of the string s index
Some common methods of strings
Method | describe |
---|---|
len(s) | Gets the length of the string s |
+or fmt.Sprintf | Split String |
strings.Split(s,seq) | Separating characters s with seq separator |
strings.Contains(s,subs) | Query string s for substring subs |
strings.HasPrefix(s,prefix)/strings.HasSuffix(s,suffix) | Determines whether the prefix/suffix is the specified string |
strings.Index(s,subs)/strings.LastIndex(s,subs) | The index position of the first (last) occurrence of the query substring subs in s, or -1 if none |
strings.join(s_arr,seq) | String arrays are stitched together into a string using seq |
//Split String fmt.Printf("strings.Split(s, \" \"): %v\n", strings.Split(s, " ")) //Query whether a string contains the specified string fmt.Printf("strings.Contains(s, \"student\"): %v\n", strings.Contains(s, "student")) //Determines whether the prefix is a specified string fmt.Printf("strings.HasPrefix(s, \"hello\"): %v\n", strings.HasPrefix(s, "hello")) //Determines whether a suffix is a specified string fmt.Printf("strings.HasSuffix(s, \"student.\"): %v\n", strings.HasSuffix(s, "student.")) //Finds the index position for the first occurrence of a specified string fmt.Printf("strings.Index(s, \"a\"): %v\n", strings.Index(s, "aaa")) //Finds the index position for the last occurrence of a specified string fmt.Printf("strings.LastIndex(s, \"a\"): %v\n", strings.LastIndex(s, "a")) //Stitching String Array fmt.Printf("strings.Join([]string{\"i\", \"am\", \"a\", \"student.\"}, \" \"): %v\n", strings.Join([]string{"i", "am", "a", "student."}, " "))
Run Results
strings.Split(s, " "): [I am a student.] strings.Contains(s, "student"): true strings.HasPrefix(s, "hello"): false strings.HasSuffix(s, "student."): true strings.Index(s, "a"): -1 strings.LastIndex(s, "a"): 5 strings.Join([]string{"i", "am", "a", "student."}, " "): i am a student.
Synchronized updates to personal blog system: Basic data types for the golang learning notes series