Golang_method_ factory pattern

Golang

1 method

Struct is generally not in Golang definition method, it is generally further defined method.
Example, the following methods I Person defines three methods;
the first method of the output p1 num
second method of adding the digital outputs 0-n and the
third method is the output of the two numbers and
The third method ends with return int then there will be a reception.

package main
import "fmt"
//方法
type Person struct{
	num int
}
func (p1 Person) test(){
	fmt.Println(p1.num)
}
func (p Person) test2(n int){
	res := 0
	for i := 0;i < n;i++{
		res += i
	}
	fmt.Println(res)
}
func (p Person)test3(n1 int, n2 int)int{
	return n1 + n2
}
func main(){

	var p Person
	p.num = 14
	p.test()
	p.test2(100)
	res := p.test3(10,20)
	fmt.Println(res)
}

Result
14
4950
30

He stressed 1
in Golang the methods and functions if it is passed, then the value of the efficiency will be lower
so Golang designers design the type of incoming pointers and more efficient.

type c1 struct{
	num float64
}

func (c2 c1) area() float64{
	return 3.14 * c2.num * c2.num
}
func (c2 *c1)area1() float64{
	return 3.14 * (*c2).num * (*c2).num
}
//和上面的方法是一样的
//只是因为在go语言的底层有优化
func (c2 *c1)area2()float64{
	return 3.14 * c2.num * c2.num
} 

func main(){
	var r c1
	r.num = 4.0
	fmt.Println(r.area())
	var r2 c1
	r2.num = 5.0
	fmt.Println((&r2).area1())
	fmt.Println(r2.area2())
	fmt.Println(r2.area1())
}

The results
50.24
78.5
78.5
78.5
The above is a pointer passed, but due to optimization, so the assignment and input parameters You never need to add * and &

2 emphasize
the following method is tostring method is equivalent to java's string method output

type student struct{
	Name string
	Age int
}
//定义String方法:
//可以输出固定的形式
func (stu *student)String()string{
	str := fmt.Sprintf("Name=[%v] Age=[%v]",stu.Name,stu.Age)
	return str
}
func main(){
	stu := student{"张三",18}
	fmt.Println(&stu)
}

Results
Name = [Zhang] Age = [18]

Emphasize 3
golang supports object-oriented language
following method is an instance of an object-oriented

//面向对象的实例:
type student struct{
	name string
	age int
	sex string
	id int
}
func (stu *student)say()string{
	str := fmt.Sprintf("name=[%v],age=[%v],sex=[%v],id=[%v]",stu.name,stu.age,stu.sex,stu.id)
	return str
}
func main(){
	var stu = student{
		name : "tom",
		age : 16,
		sex : "男",
		id : 001,
	}
	fmt.Println(stu.say())
}

Results
name = [tom], age = [16], sex = [ M], id = [1]

2 factory pattern

In general the book if it is a different package, then Golang If you need to introduce if the structure of another package requires the lead pack and the first letter of structure required capital on behalf of the public
but some programmers do not want to use uppercase letters, but the small letters are private private, use can be introduced into the structure of the plant model to private mode.

Here Insert Picture Description

student.go

package model

type student struct{
	Name string
	Age int
}

func NewStudent (n string,a int) *student{
	return &student{
		Name : n,
		Age : a,
	}
}

main.go

package main
import "fmt"
import "go_code/project09/chapter04/demo01/model"
func main(){
	
	var stu = model.NewStudent("tom",15)
	fmt.Println(*stu)
	fmt.Println(stu.Name,stu.Age)
}

Results
{15} Tom
Tom 15

He stressed 1
in the above engineering model should be emphasized that the structure of the property must be capitalized before they can be accessed. public
if the first letter is lowercase, then you must also use the factory pattern
as the Age -> age

student.go

type student struct{
	Name string
	age int
}

func NewStudent (n string,a int) *student{
	return &student{
		Name : n,
		age : a,
	}
}
func (a *student)Getage()int{
	return a.age
}

main.go

package main
import "fmt"
import "go_code/project09/chapter04/demo01/model"
func main(){
	
	var stu = model.NewStudent("tom",15)
	fmt.Println(*stu)
	fmt.Println(stu.Name,stu.Getage())
}

3 summary

The factory pattern here is quite the package and golang.

Published 101 original articles · won praise 12 · views 6187

Guess you like

Origin blog.csdn.net/qq_43141726/article/details/104372924