We welcome you to subscribe, read, comment and like!!!!

preface

I have been a little busy recently and haven’t had time to update. But the pace of learning, should not stop for any reason. So, today we’re going to continue, we’re going to do a little bit more, catch up with the progress of learning.

Previously, we talked about the concept and examples of new in Go and Java in the slow talk about the micro comparison of Go GoVSJava (7). We learned, in layman’s terms, about object creation. So how do you define the behavior of an object?

Similarly, we have described the use of Map data structures in Go in detail. See Map | Go Theme month in Slow Talk Go and Map | Go Theme month in Slow talk Go. Today, we’ll also briefly compare Go to Java’s collection containers.

Go related data containers compared to Java’s collection framework

Go has some data structures: array, slice, map, bidirectional linked List, circular linked List, heap Go’s own library does not have set, no set (List), but the third-party library has implementation. In Java are: Map, Set, List, Queue, Stack, the concept of arrays in Java without slicing. The array printing format in Go is [1,2,3,4,5]. The slice printing format in Go is [[1,2,3]. The Go map is roughly the same as the Java map

Go function, Go method and Java method comparison

Function definition in Go

Go can have multiple return values, unlike Java, where multiple return values are worth encapsulating into an entity or map. Func function name (variable 1 variable type, variable 2 variable type…) Return value 1 type 1, return value 2 type 2… {// Note that the closing bracket of this method must be on the same line as func

Example 1:

package main

import "fmt"

func main(a) {
   // Define local variables
   var a int = 100
   var b int = 200
   var result int

   // Call the function and return the maximum value
   result = max(a, b)

   fmt.Println( "The maximum is :", result )
}

/* Returns the maximum value of two numbers */
func max(num1, num2 int) int {
   /* Define local variables */
   var result int

   if (num1 > num2) {
      result = num1
   } else {
      result = num2
   }
   return result
}
Copy the code

Example 2: Return multiple values

package main

import "fmt"

func main(a) {
   a, b := swap("li_ming"."xiao_hong")
   fmt.Println(a, b)
}

func swap(x, y string) (string.string) {
   // Return multiple values
   return y, x
}
Copy the code

Note: Function parameters: Base types are passed by value, complex types are passed by reference

Example 3: Parameters to a function: variable length parameter passing

package main

import "fmt"

func main(a) {
	manyArgs(1.2."2"."3"."4")
	manyArgs(1.2."5"."5"."5")
	dataStr := []string{"11"."11"."11"}
	// Pass array is also ok, add three dots
	manyArgs(1.2,dataStr...)
}

// Variable arguments must be placed last
func manyArgs(a int,b int ,str ...string ){
	for i,s := range str {
		fmt.Println(i,s)
	}
}
Copy the code

Note: The return value of the function: if there is a return value, the type of the return value must be written. The name of the return value variable may or may not be written as appropriate.

Example 4: defer: Defer execution (similar to finally in Java)

package main

import "fmt"

func main(a) {
   testMyFunc();
}


func testDefer1(a) {
   fmt.Println("print defer1")}func testDefer2(a) {
   fmt.Println("print defer2")}func testMyFunc(a) {
   //defer is executed before the method returns, a bit like finally in Java
   // Defer can be written anywhere, and if multiple defer defer will be executed in reverse order
   defer testDefer2()
   defer testDefer1()
   fmt.Println("print my func")}Copy the code

Example 5: Discard the return value

package main

import "fmt"

func main(a) {
   // Method 1 Discard: discard num1 and STR
   _,num2,_:= testFun(1.2."3");
   fmt.Println(num2)
   // Mode 2 discard:
   _,num3,_:= testFun(1.3."4");
   fmt.Println(num3)
}

func testFun(num1,num2 int,str string) (n1 int,n2 int,s1 string){
   n1 = num1
   n2 = num2
   s1 = str
   return
}
func testFun2(num1,num2 int,str string) (int.int.string){
   return num1,num2,str
}
Copy the code

2). Method definitions in Java

Access modifier returns value type method name (parameter 1 type parameter 1, parameter 2 type parameter 2...) {return return value; }Copy the code

Example:

 public Integer doSomething(String name,Integer age) {
    
 		return 20;
 }
Copy the code

conclusion

Time is fast, today, our study task should end. Today’s learning, I believe you will have further growth. See you tomorrow. Come on, every young man who pursues his dream.