Home >Backend Development >Golang >Does golang have a factory class?

Does golang have a factory class?

青灯夜游
青灯夜游Original
2023-01-03 16:58:593907browse

Golang does not have a factory class because golang does not support classes. There is no clear object-oriented statement in golang, and it cannot be regarded as an object-oriented language because: 1. golang only supports encapsulation and does not support inheritance and polymorphism; 2. golang only has struct and no class. Of course, struct can also be compared to class in other languages.

Does golang have a factory class?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Golang does not have a factory class because golang does not support classes.

There is no clear object-oriented statement in golang, and it cannot be regarded as an object-oriented language. Because:

  • golang only supports encapsulation and does not support inheritance and polymorphism

  • golang only has struct and no class

If you really want to make a connection, you can compare struct to class in other languages.

Class declaration

type Poem struct {
    Title  string
    Author string
    intro  string
}

This declares a class without public, protected, or private declarations.

Golang uses another approach to implement attribute access permissions: if the initial letter of the attribute is capitalized, it can be accessed in other packages, otherwise it can only be accessed in this package. The same goes for class declarations and methods.

Class method declaration

func (poem *Poem) publish() {
    fmt.Println("poem publish")
}

or

func (poem Poem) publish() {
    fmt.Println("poem publish")
}

is different from other languages. The golang declaration method is the same as the ordinary method, except that it is added after func poem Poem such a statement. The difference between adding and not adding * is that one is passing a pointer object and the other is passing a value object.

【Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of Does golang have a factory class?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:Is golang Chinese?Next article:Is golang Chinese?