To recap, I shared the basic syntax of GO, including variable definitions, primitive types, conditional statements, and loops. This article will start with you to synchronize with the basic GO language progress.

Definition of a function

Last time, I actually wrote a bunch of functions in a bunch of demos, but I didn’t make it clear what the function was. Now we’re going to do the same thing, look at the code.

func calculate(a,b int, op string) int {
	switch op {
	case "+":
		return a + b
	case "-":
		return a - b
	case "*":
		return a * b
	case "/":
		return a / b
	default:
		panic("unsupported op")}}Copy the code

The above is a relatively simple calculation of two integer plus, minus, multiplication and division of a function, first we can see that the definition of the function is also following the definition of the variable, let’s define the name of the function, and then the return value of the function. Of course, the same is true of parameter definitions in functions.

In addition, the GO language has an interesting operation compared to other languages, which is that it can have multiple return values. For example, let’s write a division example, which you learned in elementary school when you have a remainder when you can’t divide. Now let’s look at a function.

func div(a int, b int) (int.int){
	return a / b, a % b
}
Copy the code

What do you think of the return value above? In fact, the two final return values do not reflect any business significance, we can not distinguish the final return result exactly what is used. Of course, GO also discovers this drawback, so the name of our return value can be defined as follows. We name the quotient of division as q and the remainder as r, so we can get the following after improvement:

func div(a int, b int) (q ,r int){
	return a / b, a % b
}
Copy the code

And if that’s the case we can get the result from the main call

func main(a) {
	fmt.Println(div(4.3))
	q,r := div(5.6)
	fmt.Println(q,r)
}
Copy the code

Now, the problem is, if we want only one of the quotient, and we don’t want the remainder, how do we write this, because we know that in go syntax, we have to define variables after the variable, otherwise we will get a compiler error, so we can just use “_” instead. The specific code block is as follows

func main(a) {
	q,_ := div(5.6)
	fmt.Println(q)
}
Copy the code

So we can just get one of these values.

Actually GO functional programming language programming language, function is very important, so let’s again a little bit high-end function of writing is to function as a parameter to the function itself, said more around, actually itself began to accept also is a little hard to understand, old cat but first, let me write the example here, we try to understand it, Of course, the old cat will have a more detailed introduction to functional programming later, specific examples are as follows

func apply(op func(int.int) int.a.b int) int{
	fmt.Printf("Calling %s with %d,%d\n",
		runtime.FuncForPC(reflect.ValueOf(op).Pointer()).Name(),a,b)
	return op(a,b)
}
Copy the code

Let’s make a brief summary of the GO language’s functions:

  • The return value type follows
  • Multiple values can be returned
  • Functions can be arguments
  • No default arguments, variable arguments, overloading, etc

Pointer to the

Related to the definition

Most of the students concerned about the old cat should be software majors, I don’t know if you are familiar with C language, C language actually has Pointers, Pointers in C language is relatively difficult. In fact, GO language also has Pointers, relatively simple, because GO language Pointers can not be calculated.

A pointer is simply a pointer to the memory address of a value.

In GO, the de-address character is &, which returns the memory address of the variable if placed before it. Here’s an example:

package main

import "fmt"
func main(a) {
   var a int = 10  
   fmt.Printf("Variable address: %x\n", &a  )
}
Copy the code

This is actually the GO language address access method, so how do we access it? Here comes our pointer, as shown in the following code example

var var_name *var-type
Copy the code

Var-type is the type of a pointer, var_name is the name of a pointer variable, and * is used to specify whether the variable is a pointer. So let’s look at the following example:

var ip *int        /* points to an integer */
var fp *float32    /* points to floating point */
Copy the code

So this essentially defines two Pointers to int and float32.

Using a pointer

package main

import "fmt"

func main(a) {
   var a int= 20   /* Declare the actual variable */
   var ip *int        /* Declare a pointer variable */

   ip = &a  /* The storage address of the pointer variable */

   fmt.Printf("The address of variable A is: %x\n", &a  )

   /* The storage address of the pointer variable */
   fmt.Printf("IP variable stored pointer address: %x\n", ip )

   /* Use Pointers to access values */
   fmt.Printf("* IP variable value: %d\n", *ip )
}
Copy the code

So what we get is zero

The address of the a variable is: 20818a220 the address of the pointer to the IP variable is: 20818a220 * the value of the IP variable is: 20Copy the code

GO actually has a null pointer, so when a pointer is defined and no variables are assigned to it, its value is nil. Nil Pointers are also called null Pointers. Nil is conceptually the same as null, None, nil, and null in other languages. A pointer variable is usually abbreviated to PTR.

The following example

package main

import "fmt"

func main(a) {
   var  ptr *int
   fmt.Printf("PTR value: %x\n", ptr  )
}
Copy the code

The results of

The value of PTR is 0Copy the code

So our general judgment for a null pointer is

if(ptr ! =nil)     /* PTR is not null */
if(ptr == nil)    /* PTR is null */
Copy the code

Above is the old cat to take you into the pointer of the door, of course, is the old cat’s entry. We’ll see how Pointers are used in practical examples later on.

Value passing and reference passing

So what is value passing and what is reference passing? Let’s briefly look at a piece of C++ code, as follows:

void pass_by_val(int a) {
   a++;
}
void pass_by_ref(int &a){
  a++;
}
int main(a){
   int a = 3;
   pass_by_val(a);
   printf("After pass_by_val:%d\n",a);
   pass_by_ref(a);
   printf("After pass_by_ref:%d\n",a);
}
Copy the code

The above two methods, one is passed by value and the other is passed by reference, so what is the final output? You can think about it a little bit. The answer is actually 3 up here and 4 down here, so why?

In this example, the function copies the value of main into the function, adding a 1 to the function, but the original value is still 3, so the final output is still 3.

If we look at another method, passing by reference, from the point of view of the input parameter, the inside a and the outside a refer to the same address, so when the inside function increments a, the outside function a changes to 4.

So is our GO passing by value or by reference? Actually, the GO language only has value passing.

You might be a little confused, but a lot of times, you don’t have to worry too much about it, because in the real world, you can just use the value of the return function.

Write in the last

Above, in fact, the old cat shared with you the function definition of GO language, as well as a more important pointer concept, in the later study, we will be more in-depth to experience. In practice to slowly deepen the impression. Of course, the above example also hope that you can follow it to write, run experience. If you don’t understand, welcome to communicate and make progress together. I am an old cat, more content, welcome you to search attention to the old cat public number “programmer old cat”.