This is a creation in Article, where the information may have evolved or changed.
There is no explicit object-oriented argument in the Golang, and it is true that structs can be compared to classes in other languages.
class declaration
Type Poem struct {Title stringauthor stringintro String}
This declares a class in which there is no public, protected, private declaration. Golang uses a different approach to access the property: The first letter of the property is capitalized and can be accessed in other packages, otherwise it is only accessible in this package. This is also true of the Declaration and method of the class.
class method declaration
Func (poem *poem) publish () {FMT. Println ("Poem Publish")}
Or
Func (poem poem) publish () {FMT. Println ("Poem Publish")}
Unlike other languages, Golang declares that the method is consistent with the normal method, but adds a declaration such as poem *poem after Func. The difference between plus * and no plus * is that one is passing a pointer object and one is passing a value object.
Instantiating an Object
There are several ways of instantiating an object
Poem: = &poem{}poem. Author = "Heine" poem2: = &poem{author: "Heine"}poem3: = new (Poem) poem3. Author = "Heine" poem4: = poem{}poem4. Author = "Heine" POEM5: = Poem{author: "Heine"}
The property value can be initialized when instantiated, default to system default if not indicated
Add & Symbol and new is a pointer object, no is a value object, this is inconsistent with PHP, Java, when passing the object to decide whether to pass the pointer or value.
Tips: Passing pointers is not a good deal when objects are small.
constructor function
To view the official documentation, Golang does not have a constructor to say. If you must do some work when initializing an object, you can encapsulate the method that generated the instance yourself.
Func newpoem (param string, p. interface{}) *poem
Example:
Func Newpoem (author string) (poem *poem) {poem = &poem{}poem. Author = Authorreturn}poem6: = Newpoem ("Heine")
Inherited
Specifically, Golang is called a combination (composition).
Type Poem struct {Title stringauthor stringintro string}type prosepoem struct {poemauthor string}
The poem is declared in the Prosepoem property, which represents the properties and methods that combine poem. Can be called as follows:
Prosepoem: = &prosepoem{}prosepoem.author = "Heine"
If there is a conflict in the attribute, the periphery is the primary.
Type prosepoem struct {poemauthor string}
When accessing author, the default is Prosepoem author, if you need to access Poem author properties can be accessed using ProsePoem.Poem.Author.
Prosepoem: = &prosepoem{}prosepoem.author = "Shelley" ProsePoem.Poem.Author = "Heine" FMT. Println (Prosepoem)
This can be seen intuitively from the output.
&{{Heine} Shelley}
The inheritance and properties of the method are consistent and are no longer listed here. By combining the words can be very good to achieve multiple inheritance.
Method overloading
A method overload is a class in which you can have the same function name, but their arguments are inconsistent, which is common in Java and C + +. Golang If you try to do this will report a re-declaration (redeclared) error, but the Golang function can declare indeterminate parameters, which is very powerful.
Func (poem *poem) recite (v. ... interface{}) {FMT. Println (v)}
where v ... interface{} indicates the indefinite meaning of the parameter, where V is the slice type, FMT. The Println method is also defined in this way. If you want to implement different functions according to different parameters, you need to detect the passed parameters within the method.
Interface
An important thing about object-oriented is the interface, and the interface in Golang is not the same as other languages, and it is one of the Golang values of the design. Learn more about the interface it will take a while to share it next time.
Complete sample code Download: Golang Object-oriented sample code
Reprint Please specify: Happy programming»golang Object-oriented thinking and implementation