[go language introduction series] (VIII) is go language an object-oriented language?

Time:2021-9-19

[go language introduction series] previous articles:

  • [go language introduction series] (V) use of pointers and structures
  • [go language introduction series] (VI) further exploration of functions
  • [go language introduction series] (VII) how to use go?

1. Is go an object-oriented language?

In [go language introduction series] (VII) how to use go? This article has introduced the concept of method, but this method is not actually an object-oriented method. Method is actually a new behavior added to the type defined by the user. It is also a function.

There is an answer to this question in the official document:

Yes and no. Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy. The concept of “interface” in Go provides a different approach that we believe is easy to use and in some ways more general. There are also ways to embed types in other types to provide something analogous—but not identical—to subclassing. Moreover, methods in Go are more general than in C++ or Java: they can be defined for any sort of data, even built-in types such as plain, “unboxed” integers. They are not restricted to structs (classes).

Also, the lack of a type hierarchy makes “objects” in Go feel much more lightweight than in languages such as C++ or Java.

Neither.Although go has types and methods and allows object-oriented programming, it has no type hierarchy。 In go, the concept of “interface” provides different methods. We believe it is easy to use and more general in some aspects. There are also ways to embed types in other types to provide similar (rather than identical) things for subclassing. In addition, the methods in go are more general than those in C + + or Java: they can be defined as any kind of data. Even built-in types like ordinary “unboxed” integers. They are not limited by structures (classes).

In addition,The absence of type hierarchy also makes “objects” in go feel lighter than those in C + + or Java

With this answer, the concepts of “inheritance” and “Rewriting” introduced below are not strictly the concepts of inheritance and rewriting in object-oriented. Here, only these two nouns are used to express the two characteristics of go.

2. “Succession”

In object-oriented, inheritance is the relationship between subclass and parent class. Subclass will inherit the public member variables and member methods of parent class.

As mentioned earlier, go allows object-oriented programming. How does go “inherit”?

2.1. Inheritance field

In [go language introduction series] (V) the use of pointers and structures, anonymous fields (also known as embedded fields) are introduced:

package main

import "fmt"

type people struct {
	name string
	age int
}

type student struct {
	people
	school string
}

func (s student) say() {
	FMT. Printf ("I'm% s, I'm% d years old, and I go to school in% s.", s.name, s.age, s.school)
}

func main() {
	Stu: = student {people {"xingxiaoguan", 1}, "sunshine primary school"}
	stu.say()
}

function:

I'm xingxiaoguan. I'm 1 years old and I go to sunshine primary school

structural morphologystudentAnonymous field inpeople, sostudentThere it ispeopleofnameandageField. When an anonymous field is a structure, all fields of the structure will be introduced into the current structure. Is this much like inheritance in object-oriented? The child class inherits the public member variables of the parent class.

Consider the following question ifstudentandpeopleAll of themnameField, how does the go language handle when accessing?

package main

import "fmt"

type people struct {
	Name string // person name
	age int
}

type student struct {
	people
	Name string // student name
	school string
}

func main() {
	Stu: = student {people {"Li Ergou", 1}, "Li Qianxiang", "Sunshine School"}
	FMT. Println (stu. Name) // Li Qianqian
	FMT. Println (stu. People. Name) // Li Ergou
}

In this case, there is a field conflict. Go will access the outer field first. For example,stu.nameyesLi Qianxiang(outer layer),stu.people.nameyesLi Ergou(inner layer).

2.2. “Inheritance” method

We bind the function to the structure type through the receiver. Such a function is called a method, and the method conceptually belongs to the structure corresponding to the receiver.

The effect of “inheriting” the field is realized through the anonymous field in the structure. It is also possible for the method. Here is an example:

package main

import "fmt"

type people struct {
	name string
	age int
}

type student struct {
	people
	school string
}

type programmer struct {
	people
	language string
}

func (p people) say() {
	FMT. Printf ("I'm% s, and I'm% d years old. Learn go language with me! \ n", p.name, p.age)
}

func main() {
	Stu: = student {people {"xingxiaoguan", 1}, "sunshine primary school"}
	stu.say()
	Prom: = programmer {people {"Zhang San", 1}, "Blue Sky Construction Co., Ltd.}
	prom.say()
}

function:

I'm xingxiaoguan. I'm 1 years old. Let's learn go language with me!
I'm Zhang San. I'm 1 year old. Learn go language with me!

say()The recipient of the method ispeopleType, and structurestudentandprogrammerThere are anonymous fields inpeople, sostuandpromCan be calledsay()method. Is this much like an object-oriented language subclass inheriting the public method of the parent class?

3. “Rewrite”

3.1. Override field

Previously, we have described how to deal with the conflict between the structure and the field of the structure as its field. With this feature, we can “override” fields.

package main

import "fmt"

type people struct {
	Name string // milk name
	age int
}

type student struct {
	people
	Name string // name
	school string
}

func (s student) say() {
	FMT. Printf ("I'm% s, and I'm% d years old. Learn go language with me! \ n", s.name, s.age)
}

func main() {
	Stu: = student {people {"Li Ergou", 1}, "Li Qianxiang", "sunshine primary school"}
	stu.say()
}

function:

I'm Li Qianxiang. I'm 1 year old. Let's learn go language with me!

Li Ergou is a nickname, Li Qianxiang is a nickname. When I introduced myself, I said Daming Li Xiangqian. If a nickname is required, usestu.people.name

3.2. “Rewrite” method

Here is an example:

package main

import "fmt"

type people struct {
	name string
	age int
}

type student struct {
	people
	school string
}

type programmer struct {
	people
	language string
}

Func (P people) say() {// the say method of people
	FMT. Printf ("I'm% s, and I'm% d years old. Learn go language with me! \ n", p.name, p.age)
}

Func (s student) say() {// student overrides the say method of people
	FMT. Printf ("I'm% s, a student, and I'm% d years old. I go to school in% s! \ n", s.name, s.age, s.school)
}

Func (P programmer) say() {// programmer rewrites the say method of people
	FMT. Printf ("I'm% s, a programmer, and I'm% d years old. I use% s language! \ n", p.name, p.age, p.language)
}

func main() {
	Stu: = student {people {"Li Qianxiang", 1}, "sunshine primary school"}
	stu.say()
	Prmger: = programmer {people {"Zhang San", 1}, "go"}
	prmger.say()
}

function:

I'm Li Qianxiang. I'm a student. I'm 1 years old. I go to sunshine primary school!
I'm Zhang San, a programmer. I'm 1 years old. I use go language!

studentandprogrammer“Inherit”peopleofsay()Method, but not appropriate, so they “rewrite” each othersay()method.

See here, you will understand the meaning of those two sentences in the official documents.

“Although go has types and methods and allows object-oriented programming, it has no type hierarchy”

“The absence of type hierarchy also makes” objects “in go feel lighter than those in C + + or Java”

Introduction to the author

My official account is Java, Go, data structure and algorithm, computer foundation and other related articles.


This article has been included in a series of articles “go language introduction series”. This series starts from the basics of go language and is suitable for beginners from scratch.


Welcome to pay attention. Let’s embark on the journey of programming together.

Please correct any errors.

Recommended Today

XSS labs clearance challenge

XSS labs clearance challenge 0x00 xss-labs I’ve been watching XSS recently. Today I’ll do XSS labs pass the challenge. After looking for the source code for a long time, I finally found it. Because everyone knows the download speed on GitHub, I directly transferred it to my code cloud and posted it here. You are […]