Home>Article>Backend Development> Is golang object-oriented?
#In the Go language, although the concept of object-oriented is not explicitly proposed, based on the existing syntax design, we can also write object-oriented code. Object-oriented in go language is implemented with the help of struct structure.(Recommended Learning:Go)
The two key types of the object -oriented are struct and interface. Among them .
Unlike C which uses public/protected/private to indicate the visibility of members and methods, Golang uses uppercase and lowercase letters to indicate visibility, that is, members/methods starting with capital letters are visible to the outside world, while those starting with lowercase letters are private to the class. Members are not directly accessible from the outside.
In addition, there is an important difference between Golang and C in the definition of class types, that is, Golang only needs to declare the member variables of the class in the struct, and does not need to declare or define all methods in the class definition body. , method definitions are all completed outside the struct.
Golang defines the attributes of the class through struct and defines the methods of the class by passing in the class object when defining func. The public/private attributes of the attributes and methods are determined by the case of the first letter. of.
type Student struct { name string age int major string }
Golang does not fully implement inheritance, but implements it through combination. A composite class (subclass) can directly call the public methods of the combined class (base class) and access the public properties of the base class. Subclasses can also define their own properties and implement their own unique methods.
One of Golang's design philosophies is simplicity. The public/private attributes of members/methods are distinguished by case, and inheritance is realized through combination, which are all embodiments of the simplicity philosophy.
Golang’s interface type defines an abstract base class, which is a collection of methods. Any type that fully implements these methods is called the interface. accomplish. Because abstraction and polymorphism complement each other, or the purpose of abstraction is to achieve polymorphism.
The above is the detailed content of Is golang object-oriented?. For more information, please follow other related articles on the PHP Chinese website!