Home>Article>Backend Development> Does golang have classes?
Does golang have classes?
Golang has no 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. Golang declaration method is different from ordinary The method is the same, except that a statement like poem Poem is added after func. 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:golang tutorial
The above is the detailed content of Does golang have classes?. For more information, please follow other related articles on the PHP Chinese website!