This is the fourth day of my participation in the More text Challenge. For details, see more text Challenge

function

Function declaration

// Declare a function
func funcName(params paramType) result {
  // body ...
}

// Multiple arguments of the same type
func funcName(p1, p2 paramType) result {
  // body ...
}

// A variable parameter function declaration
func funcName(p1 ...interface{}) result {
  // body ...
}

// Multiple return values
func funcName(p1, p2 paramType) (result1, result2) {
  // body ...
}

// Named return parameter
func funcName(p1 paramType) (sum int,err error){
  // Sum is also a variable inside the function
  sum = 1
}
Copy the code
  • Declare a function using the keyword func

  • The function name

  • Function arguments (one or more, or none)

    • The types of multiple parameters are the same and can be unified

    • The number of parameters allowed is variable. The type of the variable parameter is slice

      // Declare a function with variable arguments
      func sayHi(name.string){
        for val,_ := range name{
          fmt.Println("hi,",val)
        }
      }
      
      // Use a function with variable arguments
      sayHi("xiaohong"."xiaoming")
      
      
      Copy the code
  • Result is the function value returned. It is used to define the return value type. Multiple return values are supported. Do not write if there is no return value.

    • Multiple return values are supported, extended with parentheses. In a function, return multiple values separated by commas (,)
    • When receiving multiple return values, you can use “_” to ignore it if it is not needed, such as result, _ = sum(1,2).
    • Named return parameters, that is, the function return value can also have a variable name, and the return value can be used as a variable within the function

Package level function

In Go, functions are dependent on a package.

  • Functions within the same package can be called
  • Functions from different packages can only be called if they are public

In Go, there are no public or private modifiers to indicate that a function is public or private. Instead, the first letter of the function name is capitalized to indicate that the scope of the function is public.

  • Lowercase functions –> private functions that can only be called from the same package
  • Function uppercase –> common function, different package can also call

Anonymous functions and closures

func main(a) {
  // sum is an anonymous function (a function variable)
	sum := func(a, b int) int {
		return a+b
	}
	fmt.Println(sum(1.2))}Copy the code
  • Anonymous functions: Functions that do not have a function name
  • Internal functions: Defines anonymous functions within a function
  • An internal function defined within a function can use variables of an external function, which is called a closure

methods

The difference between functions and methods

Methods are similar to functions, with one major difference: methods must have a receiver

Recipient is a type, and the method must be bound to a certain type. A method is a certain type of method

So, functions belong to a package and methods belong to a type

SayHi is bound to the Name type
type Name string
func (name Name)sayHi(a){
	fmt.Println("name is ",name)
}

// The binding can pass. Way to use
name := Name("jasen")
name.sayHi()
Copy the code
  • Declare a Name type with the keyword type
  • Methods and types are bound primarily by adding a receiver (name name) to the method name sayHi.
  • To define the receiver’s method, click. Way to use

Value type receiver and pointer type receiver

// Bind the pointer type receiver
func (name *Name) updateName(a) {
	*name = Name("yang")}// Bind the value type receiver
func (name Name) updateName2(a) {
	name = Name("yang2")}func main(a) {
	name := Name("jasen")
	name.sayHi()

	name.updateName2()
	name.sayHi()
  
	name.updateName()
	name.sayHi()
}

// Output the result
name is  jasen
name is  jasen
name is  yang
Copy the code

The observation shows that, based on the receiver of the value type, changing the value does not affect the original value, but the receiver of the pointer type changes the original value

In the example above, we used a value type variable to call the method. If we used a pointer type, such as (&name).updatename (), we could run the code to see the result.

Actual conclusions:

  • A pointer type variable calls a method of a value type receiver, and GO automatically converts it to a value call
  • A value type variable calls the method of the pointer receiver, and Go automatically converts to a pointer call