[Articles] code is a step by step from scratch to build their own framework golang

Foreword

import cycle not allowed

I do not know if there are a lot of new people to learn golang encountered such a problem. Package circular reference problem, very stressful, especially in the projects become more complex, so an error occurs, which means you may need to modify a lot of code, which is great for a rookie in terms of pressure. In general way, to resolve this error is that the package will need to refer to abstract the interface, after decoupling call. But this is not what I want to talk about the contents of this chapter. This series is mainly about how to prevent this problem from architecture code organization. Step by step to build their own from scratch golang framework. This chapter will achieve the following functions:

  • A common base frame, a good style of code organization;
  • It contains some basic components: log, configuration, database, and other queues;
  • Provide external http, websocket, rpc interface services.

Naming conventions

Let's be clear about naming conventions, then go start writing code. as follows:

Package name: lowercase letters, not separate words, such as package called awesomeproject, but also awesome and project composition;
File name: same package name specification;
constant: capital letters, underlined words segmentation, such as AWESOME_CONST;
variables: small hump name, such as awesomeVariable;
function name / method name: public methods of external calls, use a large hump name, such as AwesomeFunction, private methods, using a small hump.

Start

In order to facilitate future if someone needs to use this project, I first create a project on GitHub.

create-project.png

Next, go get dragged into this repository:

go get "github.com/TomatoMr/awesomeframework"

goget.png

Finally, we execute the following command to initialize for our project:

go mod init awesomeframework

summary

This, we completed the initialization of the project, the next step will be initially built our code organization.


I welcome the attention of the public number: onepunchgo , will be the compilation of relevant documents and information.
qrcode_for_gh_b6f48ecdc457_258.jpg

Guess you like

Origin blog.51cto.com/14664952/2467634