go learning - Methods

1, Method

The methods in Golang act on the specified data type (that is, bind to the specified data type). Therefore, custom types can have methods, not just struct s.

2, Use of methods

package main

import "fmt"

//Define a structure
type Person struct {
	Name string
} 
//Binding method to structure
func (person Person) test() {
	person.Name = "pony"
	fmt.Println(person)
}

func main() {
	person := Person{}
	//Call method
	person.test()
}

III. introductory exercises

1. Add the speak method to the Person structure, and the output xxx is - a good Person

2. Add a jisuan method to the Person structure, which can calculate from 1+ 1000 results

3. Give the Person structure jisuan2 method, which can receive a number N and calculate from 1 ++ Results of n

4. Add the getSum method to the Person structure to calculate the sum of the two numbers and return the result.

package main

import "fmt"

//Define a structure
type Person struct {
	Name string
}
//Binding method
func (person Person) speak(){
	fmt.Println(person.Name,"He is a good man")
}
func (person Person) jisuan() {
   var sum int = 0
	for i := 1;i <= 1000 ; i++ {
      sum += i
	}
	fmt.Println(sum)
}
func (person Person) jisuan2(n int) {
	var sum int = 0
	for i := 1;i <= n ; i++ {
		sum += i
	}
	fmt.Println(sum)
}
func (person Person) jisuan3(n1,n2 int) int {
	 return n1 + n2
}
func main() {
	person := Person{}
	person.Name = "Lao Wang"
	person.speak()
	person.jisuan()
	person.jisuan2(100)
	jisuan3 := person.jisuan3(10, 20)
	fmt.Println(jisuan3)
}

IV. method memory diagram

1. When a method is called through a - variable, its calling mechanism is the same as that of a function

2. In different places, when a variable calls a method, the variable itself will also be passed to the method as a parameter (if the variable is a value type, copy the value; if the variable is a reference type, copy the value)

V. precautions

1. Structure type is a value type. In method invocation, the value type transfer mechanism is followed, which is the value copy transfer mode

2. In the method, modifying the value of the structure variable can be handled by the structure pointer

3. The methods in Golang act on the specified data type (i.e. bind to the specified data type). Therefore, custom types can have methods, not just struct s, such as int, float32, etc

4. Method, and function - like. The first letter of the method name is lowercase and can only be accessed in this package. The first letter of the method is uppercase and can be accessed in this package and other packages.

5. If a variable implements the String() method, FMT By default, printin will call String() of this variable for output

6, Practice

1, write the structure (MethodUtils), programming a method, the method does not need parameters, print a 10*8 rectangle in the method, call this method in main method.

package main

import "fmt"

//Define a MethodUtils structure
type  MethodUtils struct {

}
//Define a method
func( utils MethodUtils) MethodUtil()  {
	for i := 0;i < 10; i++ {
		for j := 0;j < 7 ;j++  {
			fmt.Print("*"," ")
		}
		fmt.Println("*")
	}
}

func main() {
	utils := MethodUtils{}
	utils.MethodUtil()
}

2. Write a method, provide m and n parameters, and print an m*n rectangle in the method

package main

import "fmt"

//Define a MethodUtils structure
type  MethodUtil struct {

}
//Define a method
func( utils MethodUtil) MethodUtils(n, m int)  {
	for i := 0;i < n; i++ {
		for j := 0;j < m ;j++  {
			fmt.Print("*"," ")
		}
		fmt.Println("*")
	}
}

func main() {
	utils := MethodUtil{}
	utils.MethodUtils(10,8)
}

3. Write a method to calculate the area of the rectangle (long len and wide width can be received) and take it as the return value of the method. Invoke the method in the main method, receive the returned area value and print it.

package main

import "fmt"

//Define a MethodUtils structure
type  MethodUtils struct {

}

func (methodUtils MethodUtils) area(len float32, width float32) float32 {
   return len*width
}

func main() {
	utils := MethodUtils{}
	fmt.Println(utils.area(10,12.5))
}

4. Writing method: judge whether a number is odd or even

package main

import "fmt"

type  Num struct {

}

func (num Num) Sum(number int) {
    if number % 2 == 0 {
    	fmt.Println(number,"It's an even number")
	}else {
		fmt.Println(number,"It's an odd number")
	}
}
func main() {
	num := Num{}
	num.Sum(5)
}

5. Define the calculator structure to realize the four functions of addition, subtraction, multiplication and division. Implementation form 1: it is completed in four methods: implementation form 2: it is completed in one method

package main

import "fmt"

type Calcuator struct {
	Num1 float32
	Num2 float32
}

func (calcuator *Calcuator) Plus() float32 {
	 return calcuator.Num1 + calcuator.Num2

}

func (calcuator *Calcuator) Subtract() float32 {
	return calcuator.Num1 - calcuator.Num2

}

func (calcuator *Calcuator) Ride() float32 {
	return calcuator.Num1 * calcuator.Num2

}
func (calcuator *Calcuator) calcuators(num byte) float32 {
   var res float32
	switch num {
	case '+':
		res = calcuator.Num1 + calcuator.Num2
	case '-':
		res = calcuator.Num1 - calcuator.Num2
	case '*':
		res = calcuator.Num1 * calcuator.Num2
	case '/':
		res = calcuator.Num1 / calcuator.Num2
	default:
		fmt.Println("The value or symbol entered is incorrect")
	}
   return res
}
func (calcuator *Calcuator) Except() float32 {
	return calcuator.Num1 / calcuator.Num2

}


func main() {
	calcuator := Calcuator{
		14,
		52.2,
	}
	fmt.Println(calcuator.Plus())
	fmt.Println(calcuator.calcuators('-'))
}

6. Compile a method in the MethodUti ls structure, receive integers (1-9) from the keyboard, and print the corresponding multiplication table:

package main

import "fmt"

type MethodUtils struct {
}

func (methodUtils *MethodUtils) Test(num int)  {
	for i := 1; i<=num; i++ {
		for j := 1; j <= i; j++ {
			fmt.Print(" ")
			fmt.Print(i,"x",j,"=",i*j)
		}
		fmt.Println()
	}
}

func main() {
	utils := MethodUtils{}
	utils.Test(9)
}

 

Keywords: Go Back-end

Added by genics on Sat, 01 Jan 2022 08:40:46 +0200