Golang Package Inheritance

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

For my own project, for example, the directory structure of the project is this:

1, I have two projects, a project as a basic project, the methods are mostly universal.

2. There is also a project under development that needs to invoke many of the functions in the underlying project.

Now the base project has a file that contains all the functions that operate on the database, named Servicebase.go, the package name: DB

The code is as follows:

Packaeg DB

TypeServiceBasestruct{

 DBE*xorm. Engine
}

In the project under development I built a baseservice.go (package name: base) This file, the function of this file is also universal, as a base function library for my current project call, that is, the function is not designed for a certain module, but in the project many modules need to use the function.


Now I let baseservice to inherit ServiceBase , the code is as follows:

Package base

Typebaseservicestruct{

 ServiceBase
}

In this way, Baseservice inherits the function inside the ServiceBase package, and when you get a pointer to Baseservice, you can call ServiceBase through the pointer. and Baseservice inside the function.

This is not the point, the point is that the service of the other modules of this project should also inherit Baseservice, the code is as follows:

Package Foodsafety

Typefoodsafetyservicestruct{

 Baseservice
So why did you do it? Since the other modules inherit the Baseservice, they also inherit the ServiceBase, and pointers to the Foodsafetyservice structure can call ServiceBase, The functions in the three modules of Baseservice and Foodsafetyservice. Note: If you have a function with the same name in these three files, and you invoke the same name function with a pointer to Foodsafetyservice, the system invokes the function in the Foodsafetyservice file, which is the same as the object-oriented function rewrite.


Then there are people to ask, even if it is so what?
There are several benefits to doing so:
1, if your foodsafetyservice contains ServiceBase, then when you need to invoke a public function of the current project, you need to get a pointer to Baseservice, and then use this pointer to
Call public functions. However, if your foodsafetyservice contains Baseservice, this step can be omitted, which can, to a certain extent, simplify the code of the program.
2, the structure of the project is more clear.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.