Recently, when I wanted to do backend development, I turned my attention to the company s Golang. It is more convenient to use Go to do micro-services, perhaps because of Golang’s strong grammar. I have been deeply attracted by the grammar of Go.

Let’s take a look at the coquettish coding style of Go.

1. Devil variable declarations

2. Array declaration

An array is an array and is defined as follows:

var arr [n]type

In [n]type, n represents the length of the array and type represents the type of the stored element. Arrays are read and assigned in the same way as in other languages: [] :

Var arr[10]int arr[0] = 42 arr[1] = 13 var arr[10]int arr[0] = 42"The first element is %d\n", arr[0]) // Get data, return 42 ftt.printf ("The last element is %d\n", arr[9]) // Returns the last unassigned element, 0 by defaultCopy the code

Since length is also part of the array type, [3]int is of a different type than [4]int, and arrays cannot change their length. Assignments between arrays are assignments of values, that is, when an array is passed to a function as an argument, it is actually a copy of the array, not a pointer to it. If you want to use a finger pin, you’ll need to use the slice type described below.

Arrays can be declared with another :=

A := [3]int{1, 2, 3} a := [3]int{1, 2, 3} b := [10]int{1, 2, 3} Int {4, 5, 6} // Can omit the length and use '... ', Go automatically calculates the length based on the number of elementsCopy the code

3. Go’s powerful slice operation

Slice in Golang is very powerful and makes array manipulation very convenient and efficient. In development arrays of indefinite length are all slice. However, many students’ vague understanding of Slice caused them to think that the array in Golang was a reference type. As a result, they encountered many holes in the actual development, which led to some inexplicable problems. The data in the array lost the data structure of Slice, which was very simple. A pointer to the real array address PTR, the length len of slice, and the capacity Cap.

Where len and cap are the values returned by len(slice) and cap(slice) calls.

Let’s parse PTR, Len, cap according to slice’s data structure definition

// Follow the data structure defined in the figure above

type Slice struct {
    ptr   unsafe.Pointer        // Array pointer
    len   int               // slice length
    cap     int               // slice capacity
Copy the code

The sample code

4. Map declaration

Note that go is a strongly typed language, so hashMap is also typed. This is reflected in the fact that both key and value must be typed. For example, to declare a map with key as string and value as string, you need to do this

Enumeration in the GO language

5. For loop traversal

func formapTest() { var arrayi= [...]  int{1, 2, 3, 4, 5, 6, 7, 78, 9, 10}for index, c := range arrayi {
		fmt.Printf("array[%d] = %d", index, c)
	}

	str := "Go language learning and sex."
	forI, ch := range STR {fmt.Println(I, ch) //ch type rune unicode encoding} // Output: 28907 (unicode encoding, two bytes represent one character) n := len(STR)fori := 0; i < n; I ++ {ch := STR [I] / } array := []rune(STR) n = len(array)fori := 0; i < n; I ++ {ch := array[I] // The character type is byte FTt.println (I, ch) // Unicode to decimal output. // The actual data type of the character type in GOLang is uint32for} //var STR string="Yyh, hello, kaka forum, awesome."
	//fmt.Print(str)
	fmt.Print("\n================================\n")
	for i , ch :=  range str{
		//fmt.Printf("(%d, %c)",i,ch)
		fmt.Printf("(%d, %x)",i,ch)
	}

	fmt.Print(utf8.RuneCountInString(str))
	fmt.Print("================================\n")
	bytes := [] byte(str)

	//for len(bytes) > 0 {
		r, size := utf8.DecodeRune(bytes)
		fmt.Printf("%c %d",r,size)
	//}
	fmt.Println()
	fmt.Println()
	fmt.Println()
	for i,c := range bytes{

		r,_ :=utf8.DecodeRune(bytes)
		fmt.Printf("%d %c %x \n",i,r,c)
	}
	for i ,ch := range []rune(str){
		fmt.Printf("%d: %c ",i,ch)
	}

	str2 := "123 DID I issue AD FG ticket on time? Did ADFG issue A, F, SJ, DF?"
	sps   := strings.Split(str2,"")
	sps = strings.Fields(str2)
	var isContact  =  strings.Contains(str2,"You")
	fmt.Println(sps)
	fmt.Println(isContact)
}
Copy the code

Structures and inheritance in Golang

7. Golang interface

8. Empty Interface in Golang

Like Object in Java, empty interfaces (interface{}) do not contain any method, and because of this, all types implement empty interfaces. The empty interface is useless for description (because it does not contain any method), but it is useful when we need to store any type of value, because it can store any type of value. It is somewhat similar to the void* type in C.

A function that takes interface{} as an argument can take any type of value as an argument, and if a function returns interface{}, it can return any type of value. Is it very useful ah!

9. You can define fields in a structure, but not interfaces

Structs and interfaces cannot have the same API because interfaces cannot define fields. This is not a big problem because you can define getters and setters in the interface, although it’s a bit confusing. eg:

10, Public and Private naming

Golang takes Python’s public and private method naming schemes a step further. When I first discovered that functions and structures that start with uppercase letters are public and those that start with lowercase letters are private, I was surprised, but I enjoyed the syntax.

typePublicStructName struct {} //public Can be called externallytypePrivateStructName struct {} // Private structure, only internal functions can be calledCopy the code

The last

This is just the tip of the iceberg. If you want to learn golang, I recommend: github.com/Unknwon/the…

To read more

31 Android Interview Questions to Build your Foundation!

Do you choose Java, Go, or PHP for background?

AndroidUtils: The Utils Android developers have to keep in their collections

A ThreadPool called ThreadPool

Believe in yourself, there is nothing impossible, only unexpected

It’s not just technology that’s gained here!