Does golang have classes?

(*-*)浩
Release: 2019-12-13 11:11:11
Original
2860 people have browsed it

Does golang have classes?

(Recommended learning: Go

)

Golang does not have a clear object -oriented object -oriented In other words, 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
}
Copy after login

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")
}
Copy after login
or

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

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. ###

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
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!