Introduction to go language structure combination function

Release: 2020-01-13 17:14:33
forward
5477 people have browsed it

Introduction to go language structure combination function

The structure provided by Go languageis an advanced data type that combines different variables defined using various data types. Without further ado, let’s look at the example:

type Rect struct { width float64 length float64}
Copy after login

Above we have defined a rectangular structure. First, the key is that type means that a new data type is to be defined, then the new data type name Rect, and finally The struct keyword indicates that this advanced data type is a structure type. In the above example, because width and length have the same data type, they can also be written in the following format:

type Rect struct { width, length float64}
Copy after login

Okay, let’s do something with the structure and calculate the area of the rectangle.

package main import ( "fmt" ) type Rect struct { width, length float64 } func main() { var rect Rect rect.width = 100 rect.length = 200 fmt.Println(rect.width * rect.length) }
Copy after login

As you can see from the above example, the structure type is actually used in the same way as the basic data type. The only difference is that the structure type can access the internal members through. Including assigning values to internal members and reading internal member values.

In the above example, we use the var keyword to first define a Rect variable and then assign values to its members. We can also use initialization to assign values to the internal members of the Rect variable.

package main import ( "fmt" ) type Rect struct { width, length float64 } func main() { var rect = Rect{width: 100, length: 200} fmt.Println(rect.width * rect.length) }
Copy after login

Of course, if you know the order in which the structure members are defined, you can also assign values to the structure members directly in the order in which they are defined, without using the key:value method.

package main import ( "fmt" ) type Rect struct { width, length float64 } func main() { var rect = Rect{100, 200} fmt.Println("Width:", rect.width, "* Length:", rect.length, "= Area:", rect.width*rect.length) }
Copy after login

The output result is

Width: 100 * Length: 200 = Area: 20000

Structure parameter transfer method

We have said that the parameter passing method of Go function is value passing, and this sentence is also applicable to structures.

package main import ( "fmt" ) type Rect struct { width, length float64 } func double_area(rect Rect) float64 { rect.width *= 2 rect.length *= 2 return rect.width * rect.length } func main() { var rect = Rect{100, 200} fmt.Println(double_area(rect)) fmt.Println("Width:", rect.width, "Length:", rect.length) }
Copy after login

The output of the above example is:

80000 Width: 100 Length: 200
Copy after login

That is to say, although we double the width and length of the structure in the double_area function, it still does not affect the rect variable in the main function. width and length.

Structure combination function

Above we calculated the area of the rectangle in the main function, but we feel that if the area of the rectangle can be used as an "internal function of the rectangular structure “It would be better to offer. In this way, we can directly say what the area of this rectangle is without having to calculate the width and length separately. Now let's take a look at the definition method of the "internal function" of the structure:

package main import ( "fmt" ) type Rect struct { width, length float64 } func (rect Rect) area() float64 { return rect.width * rect.length } func main() { var rect = Rect{100, 200} fmt.Println("Width:", rect.width, "Length:", rect.length, "Area:", rect.area()) }
Copy after login

Huh? What kind of "internal method" is this? It's not defined inside the Rect data type at all?

Indeed, we see that although the rect variable in the main function can directly call the function area() to obtain the rectangular area, the area() function is indeed not defined inside the Rect structure. This is similar to C Languages are very different. Go uses composition functions to define structure methods for structures. Let’s take a closer look at the area() function definition above.

The first is the keyword func to indicate that this is a function, the second parameter is the structure type and instance variable, the third is the function name, and the fourth is the function return value. Here we can see that the difference between the area() function and the ordinary function definition is that the area() function has an additional structure type qualification. This way Go knows that this is a method defined for the structure.

One thing to note here is that the function defined on the structure is generally called a method.

Structures and pointers

We mentioned in the section on pointers that the main function of pointers is to change the value of the passed variable inside the function. For the above example of calculating the area of a rectangle, we can modify the code as follows:

package main import ( "fmt" ) type Rect struct { width, length float64 } func (rect *Rect) area() float64 { return rect.width * rect.length } func main() { var rect = new(Rect) rect.width = 100 rect.length = 200 fmt.Println("Width:", rect.width, "Length:", rect.length, "Area:", rect.area()) }
Copy after login

In the above example, the new function is used to create a structure pointer rect, which means that the type of rect is *Rect. When a structure encounters a pointer, you don't need to use * to access the members of the structure, just use .reference. So in the above example, we directly use rect.width=100 and rect.length=200 to set the structure member values. Because rect is a structure pointer at this time, the structure is limited to *Rect when we define the area() function.

In fact, in this example of calculating the area, we do not need to change the width or length of the rectangle, so when defining the area function, it is okay to still have the structure limit type as Rect. As follows:

package main import ( "fmt" ) type Rect struct { width, length float64 } func (rect Rect) area() float64 { return rect.width * rect.length } func main() { var rect = new(Rect) rect.width = 100 rect.length = 200 fmt.Println("Width:", rect.width, "Length:", rect.length, "Area:", rect.area()) }
Copy after login

Go is smart enough here, so rect.area() is also possible.

As for not using structure pointers and not using pointers, the starting point is the same, that is, whether you are trying to change the value of the passed parameter inside the function. Another example is as follows:

package main import ( "fmt" ) type Rect struct { width, length float64 } func (rect *Rect) double_area() float64 { rect.width *= 2 rect.length *= 2 return rect.width * rect.length } func main() { var rect = new(Rect) rect.width = 100 rect.length = 200 fmt.Println(*rect) fmt.Println("Double Width:", rect.width, "Double Length:", rect.length, "Double Area:", rect.double_area()) fmt.Println(*rect) }
Copy after login

The output of this example is:

{100 200}

Double Width: 200 Double Length: 400 Double Area: 80000

{200 400}

Structure embedded type

We can define members of another structure type inside a structure. For example, iPhone is also a Phone. Let’s look at the example:

package main import ( "fmt" ) type Phone struct { price int color string } type IPhone struct { phone Phone model string } func main() { var p IPhone p.phone.price = 5000 p.phone.color = "Black" p.model = "iPhone 5" fmt.Println("I have a iPhone:") fmt.Println("Price:", p.phone.price) fmt.Println("Color:", p.phone.color) fmt.Println("Model:", p.model) }
Copy after login

The output result is:

I have a iPhone: Price: 5000 Color: Black Model: iPhone 5
Copy after login
Copy after login

在上面的例子中,我们在结构体IPhone里面定义了一个Phone变量phone,然后我们可以像正常的访问结构体成员一样访问phone的成员数据。但是我们原来的意思是“iPhone也是(is-a)Phone”,而这里的结构体IPhone里面定义了一个phone变量,给人的感觉就是“iPhone有一个(has-a)Phone”,挺奇怪的。当然Go也知道这种方式很奇怪,所以支持如下做法:

package main import ( "fmt" ) type Phone struct { price int color string } type IPhone struct { Phone model string } func main() { var p IPhone p.price = 5000 p.color = "Black" p.model = "iPhone 5" fmt.Println("I have a iPhone:") fmt.Println("Price:", p.price) fmt.Println("Color:", p.color) fmt.Println("Model:", p.model) }
Copy after login

输出结果为

I have a iPhone: Price: 5000 Color: Black Model: iPhone 5
Copy after login
Copy after login

在这个例子中,我们定义IPhone结构体的时候,不再定义Phone变量,直接把结构体Phone类型定义在那里。然后IPhone就可以像访问直接定义在自己结构体里面的成员一样访问Phone的成员。

上面的例子中,我们演示了结构体的内嵌类型以及内嵌类型的成员访问,除此之外,假设结构体A内部定义了一个内嵌结构体B,那么A同时也可以调用所有定义在B上面的函数。

package main import ( "fmt" ) type Phone struct { price int color string } func (phone Phone) ringing() { fmt.Println("Phone is ringing...") } type IPhone struct { Phone model string } func main() { var p IPhone p.price = 5000 p.color = "Black" p.model = "iPhone 5" fmt.Println("I have a iPhone:") fmt.Println("Price:", p.price) fmt.Println("Color:", p.color) fmt.Println("Model:", p.model) p.ringing() }
Copy after login

输出结果为:

I have a iPhone: Price: 5000 Color: Black Model: iPhone 5 Phone is ringing...
Copy after login

接口

我们先看一个例子,关于Nokia手机和iPhone手机都能够打电话的例子。

package main import ( "fmt" ) type NokiaPhone struct { } func (nokiaPhone NokiaPhone) call() { fmt.Println("I am Nokia, I can call you!") } type IPhone struct { } func (iPhone IPhone) call() { fmt.Println("I am iPhone, I can call you!") } func main() { var nokia NokiaPhone nokia.call() var iPhone IPhone iPhone.call() }
Copy after login

我们定义了NokiaPhone和IPhone,它们都有各自的方法call(),表示自己都能够打电话。但是我们想一想,是手机都应该能够打电话,所以这个不算是NokiaPhone或是IPhone的独特特点。否则iPhone不可能卖这么贵了。

再仔细看一下接口的定义,首先是关键字type,然后是接口名称,最后是关键字interface表示这个类型是接口类型。在接口类型里面,我们定义了一组方法。

Go语言提供了一种接口功能,它把所有的具有共性的方法定义在一起,任何其他类型只要实现了这些方法就是实现了这个接口,不一定非要显式地声明要去实现哪些接口啦。比如上面的手机的call()方法,就完全可以定义在接口Phone里面,而NokiaPhone和IPhone只要实现了这个接口就是一个Phone。

package main import ( "fmt" ) type Phone interface { call() } type NokiaPhone struct { } func (nokiaPhone NokiaPhone) call() { fmt.Println("I am Nokia, I can call you!") } type IPhone struct { } func (iPhone IPhone) call() { fmt.Println("I am iPhone, I can call you!") } func main() { var phone Phone phone = new(NokiaPhone) phone.call() phone = new(IPhone) phone.call() }
Copy after login

在上面的例子中,我们定义了一个接口Phone,接口里面有一个方法call(),仅此而已。然后我们在main函数里面定义了一个Phone类型变量,并分别为之赋值为NokiaPhone和IPhone。然后调用call()方法,输出结果如下:

I am Nokia, I can call you!

I am iPhone, I can call you!

以前我们说过,Go语言式静态类型语言,变量的类型在运行过程中不能改变。但是在上面的例子中,phone变量好像先定义为Phone类型,然后是NokiaPhone类型,最后成为了IPhone类型,真的是这样吗?

原来,在Go语言里面,一个类型A只要实现了接口X所定义的全部方法,那么A类型的变量也是X类型的变量。在上面的例子中,NokiaPhone和IPhone都实现了Phone接口的call()方法,所以它们都是Phone,这样一来是不是感觉正常了一些。

我们为Phone添加一个方法sales(),再来熟悉一下接口用法。

package main import ( "fmt" ) type Phone interface { call() sales() int } type NokiaPhone struct { price int } func (nokiaPhone NokiaPhone) call() { fmt.Println("I am Nokia, I can call you!") } func (nokiaPhone NokiaPhone) sales() int { return nokiaPhone.price } type IPhone struct { price int } func (iPhone IPhone) call() { fmt.Println("I am iPhone, I can call you!") } func (iPhone IPhone) sales() int { return iPhone.price } func main() { var phones = [5]Phone{ NokiaPhone{price: 350}, IPhone{price: 5000}, IPhone{price: 3400}, NokiaPhone{price: 450}, IPhone{price: 5000}, } var totalSales = 0 for _, phone := range phones { totalSales += phone.sales() } fmt.Println(totalSales) }
Copy after login

输出结果:

14200

上面的例子中,我们定义了一个手机数组,然后计算手机的总售价。可以看到,由于NokiaPhone和IPhone都实现了sales()方法,所以它们都是Phone类型,但是计算售价的时候,Go会知道调用哪个对象实现的方法。

接口类型还可以作为结构体的数据成员。

假设有个败家子,iPhone没有出的时候,买了好几款Nokia,iPhone出来后,又买了好多部iPhone,老爸要来看看这小子一共花了多少钱。

import ( "fmt" ) type Phone interface { sales() int } type NokiaPhone struct { price int } func (nokiaPhone NokiaPhone) sales() int { return nokiaPhone.price } type IPhone struct { price int } func (iPhone IPhone) sales() int { return iPhone.price } type Person struct { phones []Phone name string age int } func (person Person) total_cost() int { var sum = 0 for _, phone := range person.phones { sum += phone.sales() } return sum } func main() { var bought_phones = [5]Phone{ NokiaPhone{price: 350}, IPhone{price: 5000}, IPhone{price: 3400}, NokiaPhone{price: 450}, IPhone{price: 5000}, } var person = Person{name: "Jemy", age: 25, phones: bought_phones[:]} fmt.Println(person.name) fmt.Println(person.age) fmt.Println(person.total_cost()) }
Copy after login

这个例子纯为演示接口作为结构体数据成员,如有雷同,纯属巧合。这里面我们定义了一个Person结构体,结构体内部定义了一个手机类型切片。另外我们定义了Person的total_cost()方法用来计算手机花费总额。输出结果如下:

Jemy

25

14200

更多go语言知识请关注PHP中文网go语言教程栏目。

The above is the detailed content of Introduction to go language structure combination function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!