Go learning data types and basic usage details

data type

I would like to call golang a very strong type language. Although the type declaration can be omitted, the type requirements between variables are very strict. See the data type conversion for details.

Basic types: Boolean, numeric, string, array

Other types: rune

Advanced types: Map (set), pointer, structure (there is no class in go), interface, Channel: common for concurrency, Slice, function

PS: some of the codes in the following code blocks are not a whole block of continuous code, but the verification codes of different examples after I classify them. Therefore, do not directly copy them and run them in the IDE, which will report an error Please distinguish according to comments + blank lines.

1. Boolean: true or false

 var vname bool = true

easy.

2. Number type

  • Integer types include: int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, byte

  • Floating point types include: float32 (single precision), float64 (double precision), complex64 (32-bit real and imaginary), complex128 (64 bit real and imaginary)

Number typeSign or notlengthRepresentation range
int8have1 byte (8bit)-128 ~ 127
int16have2 bytes (16bit)​ ~ ​
int32have4 bytes (32bit)​ ~ ​
int64have8 bytes (64bit)​ ~ ​
int (default)have32-bit operating system: 32bit 64 bit operating system: 64bit
bytenothing1 byte (8bit)0 -255
Single precision float32have4 bytes (8bit)-3.403E38 ~ 3.403E38
Double precision float64 (default)have8 bytes (8bit)-1.798E308 ~ 1.798E308

Unsigned integers are no longer enumerated

When an integer overflows, golang's compiler will directly report an error to you instead of displaying the overflow value; When uint and byte are assigned with a negative sign, an error is reported directly;

(contains overflows)

float64 is recommended for floating point type selection in development. complex128 is recommended for the plural type

When selecting the data type, we try to select the data type that can meet the minimum byte length of the variable.

num1 := .512   // Abbreviation: 0.512
 fmt.Println(num1)   // 0.512
 ​
 // Scientific counting method
 num2 := 1.234E2 // = 1.234 * (10^2)
 fmt.Println(num2)  // 123.4
 ​
 /* complex64 complex128 Use of type */
 var num1 complex128 = complex(50,75)
 fmt.Println(num1)    //(50+75i)
 ​
 var num2 complex128 = complex(60, 80)
 ​
 fmt.Println(num1 + num2)  //(110+155i)
 ​
 // If the real part is the same and the imaginary part is the same, the complex number is the same
 fmt.Println(num1 == num2)   //false
 ​
 // Take the value of real part and imaginary part
 var realNum = real(num1)
 var imageNum = imag(num1)
 fmt.Println("real=",realNum, "image=", imageNum)  //real= 50 image= 75

Other complex operation methods are found in math/cmplx package

3. String type: string. The bytes of the string in Go language use UTF-8 encoding to identify Unicode text.

There are strict differences between single quotation marks and double quotation marks in Go language. String types can only use double quotation marks "" or back quotation marks `, and character types can use single quotation marks'

 var str = "This is a test"

Go language uses byte to represent a single letter, which is essentially an integer (the operation is also replaced with an integer first)

 var alpha1 byte = 'a'
 var alpha2 byte = 't'
 var alpha3 byte = 'b'
 ​
 fmt.Println(alpha1) // This will print integer type: 97
 fmt.Printf("%c", alpha1)   // Format output character: a
 ​
 // Note that the following two are different, one is character 0 and the other is integer 0
 var a1 byte = '0'
 var a2 byte = 0

Go language adopts UTF-8 coding

PS: when the input string exceeds the ASCII code range (such as Chinese characters), it can be received with int type.

var hanzi1 int = 'nothing'
var hanzi2 int = 'heart'
fmt.Println(hanzi1,hanzi2)  // 26080 24515 these are the unintentional Unicode encoding of Chinese characters, and the string of Go language is encoded in Unicode
fmt.Printf("%c%c", hanzi1, hanzi2)  // Unintentionally

It's an integer, but it should be a rune data type

String type. The string is composed of one byte by one in bytes (the corresponding subscript characters can be accessed in the form of str[index] array):

var str1 = "Unintentionally"
var str2 string = "Golang"
var str3 = str1 + str2  // You can splice two strings with +
 ​
// This splicing is also right
var str = "wu" +
         "xin"
 ​
// ☆ but this spelling is wrong (+ "xin" evaluated but not used), so + should be on the top. In essence, golang will fill in a semicolon after each line
var str = "wu"
         + "xin"
 ​
 ​
// In go, the string is constant and immutable! Once assigned, it cannot be changed
str1 = "Intentional"   // Yes, because this is equal to pointing the string pointer directly to another string
//str2[0] = "J"  // can not assign to str2[0]
fmt.Println(str1)   // Intentional
 
// Use backquotes -- the string is output in its native form
var str = `wuxin\a\b\n\t\r''""\ wuxin` //Special characters in the middle will be output in native form
fmt.Println(str)  //wuxin\a\b\n\t\r''""\ wuxin

4. Other types

  • rune ≈ int32, rune represents a Unicode code point, which can be used to store Chinese

5. Array

var v_name [array_size] v_type

 // Declare and initialize
 var numbers = [10]int{1,2,3,4,5,6,7,8,9,10}
 ​
 // Assignment after declaration
 var numbers[5] float64
 numbers = [5]float64{1.1, 2.2, 3.3, 4.4, 5.5}

If the array length is uncertain, you can use Instead of the length of the array, the compiler will infer the length of the array according to the number of elements:

numbers := [...]int{1,2,3,4,5}

You can specify that subscripts are initialized to specific values, and the rest are initialized to default values

 numbers := [...]float64{0:8.8,5:8.8}
 fmt.Println(numbers)  // Output: [8.8 0 0 8.8]

Note that although I only wrote 2 during initialization, because the initial value of the element with subscript 5 is specified, the compiler defaults to 6 lengths, of which subscripts 1, 2, 3 and 4 are the default values of 0

To access data in an array:

 // Just subscript access
 numbers[0]

Multidimensional array:

var variable_name [SIZE1][SIZE2]...[SIZEN] variable_type
 var array[5][5] int = [5][5]int{{1,1,1,1,1},
     {1,1,1,1,1},
     {1,1,1,1,1},
     {1,1,1,1,1},
     {1,1,1,1,1}, }
 fmt.Println(array)

The same as C is: you cannot assign the length of an array with a variable

 var i int = 10
 var numbers = [i]int{1,2,3,4,5,6,7,8,9,10}

Error: Invalid array bound 'i', must be a constant expression

6. Map (Collection)

Map is an unordered key value pair (key - > value). Map is implemented by hash internally, so it is unordered. When we insert or take out key value pairs into the map, we cannot determine the order of key value pairs in the map.

Declaration and initialization of Map:

 // statement
 var vmap map[string] string
 ​
 // Use make declaration
 vmap := make(map[string]string)
 ​
 // Declare and initialize
 vmap := map[string]string{
     "name":"wuxin",
     "age":"21",
 }
 fmt.Println(vmap)   // Output: map[age:21 name:wuxin]

Assignment, value taking and deletion of key value pairs of Map:

vmap := map[string]string{
         "name":"wuxin",
         "age":"21",
 }
 ​
 // 1. Assignment, you can directly add new key value pairs or change the contents of existing key value pairs (change value!)
 vmap["university"] = "HIT"
 vmap["name"] = "youxin"
 ​
 // 2. Value. The first return value is a value, and the second return value represents whether this key is available? “
 name, isHas := vmap["name"]
 fmt.Println(name, isHas)   // Output: youxin true
 if(isHas){
     fmt.Println(name)
 }
 ​
 // 3. Delete key value pairs
 delete(vmap, "university")
 ​
 // 4. Traversal of map
 for key := range vmap {
     fmt.Println("key=", key, "value=", vmap[key])
 }

7. Pointer

The pointer in Golang is similar to the pointer in C language. If a pointer in Golang is defined but does not point to any address, it is a null pointer nil. The pointer in Golang can also point to variables, point to functions as function pointers, point to arrays as array pointers to access arrays, and point to pointers (pointers to pointers).

 var ptr *int
 var i int = 114514
 ptr = &i //The pointer points to the memory address of the integer variable i
 ​
 // Use the pointer to access the value of i (in fact, the value of direct access to memory)
 fmt.Println(*ptr)  // Output: 114514
 ​
 // Modify the contents of the memory address:
 *ptr = 10086
 fmt.Println(*ptr)  //Output: 10086
 ​
 // Of course, when the pointer is printed directly, the address is also output:
 fmt.Println(ptr)  //Output: 0xc000000a098

Pointer to array - "array pointer" and "pointer array":

// One dimensional array:
 ​
 // 1. Array pointer, a pointer pointing to an array:
 a := []int{1,2,3,4,5}
 var ptr *[]int = &a
 fmt.Println(*ptr)  // [1 2 3 4 5]
 ​
 // The array elements are accessed sequentially through the array pointer
 var i int
 for i=0 ;i<5;i++ {
     fmt.Println((*ptr)[i])
 }
 ​
 // 2. Pointer array, a pile of pointers, pointing to different elements respectively
 a := []int{1,2,3,4,5}
 var ptr [5]*int
 var i int
 ​
 // In fact, it is five pointers that point to five array elements in turn
 for i=0;i<5;i++ {
     ptr[i] = &a[i]
 }
 ​
 for i=0;i<5;i++ {
     fmt.Println(*ptr[i])
 }
  • Array pointer var ptr *[]int

  • Pointer array var ptr [size]*int

 // 2. Two dimensional array
 var t_array = [2][2]int{
     {1,2},
     {3,4},
 }
 var ptr *[2][2]int = &t_array
 var ptr2 = &t_array //emmm... In fact, you can omit the data type. Because of the existence of '&', ptr2 is a pointer by default!
 ​
 fmt.Println(*ptr2, *ptr)
 fmt.Println(ptr2, reflect.TypeOf(ptr2))  //&[[1 2] [3 4]] *[2][2]int

8. Structure (there is no class in go)

Struct is a collection of attributes. The data types of these attributes can be different, but struct cannot define functions (this is still different from "class"), but we can "define methods for the structure" outside the structure!

type My struct {
     name string
     age int
     university string
     major string
     vmap map[string]string // All other data types of a collection are OK
     hobbys [] Hobby  // You can also nest structure arrays
 ​
 }
 ​
 type Hobby struct {
     name string
     startYear int
 }

Declare the struct variable and initialize:

var my My = My{"wuxin",
                 21,
                 "hit",
                 "InformationSecurity",
                 map[string]string{
                         "key1":"value1",
                         "key2":"value2",
                         },
                 []Hobby{
                     {"Cartoon", 2013 },
                     {"Fiction", 2015},
                 }}
 fmt.Println(my)

Access, assignment and use of struct:

// Use "." To access structure elements
 name := my.name
 age := my.age
 fmt.Println(name, age)
 ​
 // Of course, you can modify the structure element value
 my.university = "Beijing University of Posts and Telecommunications"
 ​
 // Like Map and nested struct, they can also be accessed and modified nested
 my.vmap["key3"] = "value3"
 fmt.Println(my.hobbys[1].name)  // Output: Fiction

Define method for structure

func (my My) doing() {
     fmt.Println("studying golang...")
 }
 ​
 func (my My) getName() string {
     return my.name
 }
 ​
 func (my My) setAge(int a) {
     my.age = a
 }

9. Interface

 // Define interface
 type Pet interface{
     yell()  // If there is no return value, it can be followed by a return value type, indicating that there is a return value
 }
 ​
 // Defines the type of implementation interface
 type Cat struct {
     name string
 }
 ​
 func (cat Cat) yell() {
     fmt.Println("nya! nya!")
 }
 ​
 type Dog struct {
     name string
 }
 ​
 func (dog Dog) yell() {
     fmt.Println("wang! wang!")
 }
 ​
 ​
 func main() {
     // Define the type Cat, which implements the Pet interface, so it has its own yell() method
     var cat Pet
     cat = new(Cat)
     cat.yell()
 ​
     // Similarly, Dog
     var dog Pet
     dog = new(Dog)
     dog.yell()
 ​
 }

Although I give cat and dog a member variable of name here, because there is no Pet in the interface type, I can't access 0.0. I have to write the method setName(), getName() to operate the member variable name. By adding methods to cat and dog structures.

10. Channel: commonly used for concurrency

Pipeline is the communication mode between goroutines provided by Go language at the language level. We can use channel to pass messages between multiple goroutines. Channel is an intra process communication mode (inter thread communication mode), which does not support cross process communication. If inter process communication is required, Socket and other network modes can be used.

A pipeline can only pass one type of value, and the data in the pipeline is first in first out

Declaration: var chanName chan ElemType

var ch chan int // Define a pipeline variable ch to pass int type data.
 // That's OK
 ch := make(chan int)
 ​
 // You can even use a map so that its value is a pipe variable
 var vmap map[string]chan int
 ​
 // Write data to chan
 ch<- value
 ​
 // Read data from chan
 value := <-ch

More specifically, it is described in go concurrent programming.

11. Slice

Slice is an abstraction of an array. The length of the array is immutable. Slice abstracts the array and intercepts a certain range of the array (the original array will not be changed). The length of slice is dynamic. You can add elements to slice and use it as a dynamic array (the advantage is similar to the List handle in Java, which increases flexibility).

Define slice:

// 1. Directly define slice - it is equivalent to defining a dynamic array
 // Make ([] type, len, capacity) LEN - > length, capacity - > maximum length (capacity)
 s1 := make([]int, 10)
 ​
 // Or so, so [] int {..} without length In fact, you create slices, not arrays in the strict sense.
 s2 := []int{1,2,3,4,5,6,7,8,9,10}
 ​
 ​
 // 2. Define slices from existing arrays - equivalent to intercepting and using arrays
 var a = []int{1,2,3,4,5,6,7,8,9,10}
 ​
 // from a to z
 s1 := a[:]
 fmt.Println(s1)  // [1 2 3 4 5 6 7 8 9 10]
 ​
 // Intercept from the element with subscript 2 to the previous element with subscript 5
 s2 := a[2:5]
 fmt.Println(s2)  // [3 4 5]
 ​
 // Truncate from the element with subscript 2 to the end of the array
 s3 := a[2:]
 fmt.Println(s3)  // [3 4 5 6 7 8 9 10]
 ​
 // Intercept the previous element of the element with subscript 8 from the array
 s4 := a[:8]
 fmt.Println(s4)  // [1 2 3 4 5 6 7 8]

Basic operation of slicing:

// 1. Len() - > get slice length
 fmt.Println(len(s4))   // 8
 ​
 // 2. Cap () - > get the capacity of the slice (maximum length)
 fmt.Println(cap(s4))   // 10 (because I didn't set it, and there are 10 elements in array a, its capacity is 10 by default)
 ​
 // Set it up
 s5 := make([]int, 5, 20)
 fmt.Println(s5, cap(s5))  // [0 0 0 0 0] 20
 ​
 ​
 // 3. Append element ()
 // You can add any number of elements at a time. Don't forget to return. Only when the return value covers the original slice can you actually add a value at the end of the original slice
 s4 = append(s4,9,10)
 fmt.Println(s4)  // [1 2 3 4 5 6 7 8 9 10]
 ​
 // Of course, you can add elements and assign them to other slices, so that the original slice is unchanged.
 s5 = append(s4, 9,10,11,12)
 fmt.Println(s4)  //[1 2 3 4 5 6 7 8 9 10]
 fmt.Println(s5)  //[1 2 3 4 5 6 7 8 9 10 9 10 11 12]
 ​
 // 4. Slice copy()
 s7 := make([]int, 14, 500)
 copy(s7, s5)  // s5 copies to s7. Because the length is set to 14, the first 14 are copied (no default value is set, and the default value of int is 0)
 fmt.Println(s7,cap(s7))  // [1 2 3 4 5 6 7 8 9 10 9 10 11 12] 500

12. Functions

For every language, function is the most basic, common and extensive thing. When learning function, record it in detail.

Use of strconv

strconv is a package that integrates many format conversion functions. It is very simple. Read the manual or source code, you can fully understand the way of use.

There is a family of functions in strconv called FormatXxxx(), which is a function that converts Xxx type to string type; There is also a family of functions called ParseXxx(), which converts string type to Xxx type. You can find it according to this rule and use it as needed.

1,strconv.FormatFloat: converts a float type to a string type

strconv.FormatFloat(v_name, format mark, decimal precision, float32|float64)

Format tag:

  • 'b' (- DDP ± ddd, binary index)

  • 'e' (- d.dddde ± dd, decimal index)

  • 'E' (- d.dde ± dd, decimal index)

  • 'f' (- ddd.dddd, no index)

  • 'g' ('e': large index, 'f': other situations)

  • 'G' ('E': large index, 'f': other situations)

Usage in data type conversion

2,strconv.ParseFloat(): convert string type to float type

strconv.ParseFloat(str, float32|float64)

3,strconv. Atoi(): string to int

strconv. Itoa(): int to string

The usage is relatively simple in data type conversion. I won't elaborate. You can see the manual or the source code by ctrl+N in the IDE

ps: strconv.FormatInt() ,strconv.FormatUInt() is also a function that converts an integer type to a string type.

4,strconv. Formatbool(): convert bool type to string type

strconv.FormatBool(ture|false)

5,strconv.FormatComplex(): convert complex to string type

Data type conversion

There is no automatic data type conversion mechanism in Golang, and forced conversion is required! Whether low precision is assigned to high precision or high precision is assigned to low precision, forced conversion is required.

Forced conversion from high precision to low precision sometimes overflows and then loses data

 // float cannot directly cast int type.
 // var num1 int = int64(100.50)  error. 
 // Var num2 int = 100.505/10 is not enough
 // There seems to be no float - > int function in strconv package.
 // However, the math package can be used to realize the "rounding" of float, but it does not change the float of data type into int
 f := 123.456
 var i1 float64 = math.Ceil(f) // In fact, it is still float64 type, but the value is indeed "rounded"
 var i2 float64 = math.Floor(f)
 fmt.Println(i1, i2)  // 124 123
 ​
 ​
 // 1. int to float
 var i int = 666
 var num2 float64 = float64(i)
 fmt.Println(num2)
 ​
 // 2.int to string, using strconv package (to import)
 import "strconv"
 var i = 100
 var str = strconv.Itoa(i)
 fmt.Println(str, reflect.TypeOf(str))  //Output: 100 string
 ​
 // 3.string to int
 var str = "100"
 var i, _ = strconv.Atoi(str)   // The second return value is error code
 fmt.Println(i, reflect.TypeOf(i)) // Output: 100 int
 ​
 // 4. float to string
 var num float64 = 555.666
 var str string = fmt.Sprintf("%f", num)
 fmt.Println(str, reflect.TypeOf(str))
 ​
 // Or use strconv FormatFloat
 var num = 114.514
 // F - > format mark, 3 - > decimal part length, float - > refers to num, which is float 64 floating point number type
 var str = strconv.FormatFloat(num, 'f', 3, 64)
 fmt.Println(str)
 ​
 ​
 // 5. string to float
 // strconv.ParseFloat: converts a string to a floating point number. The second parameter is 32 - > float32, 64 - > float64
 var str = "114.514666"
 var num,_ = strconv.ParseFloat(str, 32)
 fmt.Println(num)
 ​
 // 6.bool to string 
 formatBool := strconv.FormatBool(true)
 fmt.Println(formatBool, reflect.TypeOf(formatBool)) // Output: true string

Note that it is Type(value), not (Type) value in other languages.

float64 (100) √; (float64) 100 × —— If you are used to writing C, Java may make a mistake.

_ Represents a placeholder. The return value is ignored, but it is received and cannot be used_

Data between different data types cannot be assigned to each other directly, and addition, subtraction, multiplication and division cannot be performed directly. During assignment and operation, it must be ensured that the participating variables are of the same type.

Sprintf("% specified type", variable) is very flexible. It can output variables of any specified type as string type, so it can convert any type of data type to string type

Sprintf() is recommended for converting other data types to string types

Keywords: Go

Added by daverico on Tue, 25 Jan 2022 18:16:00 +0200