Go Web framework Gin source code structure analysis

You can read this article before you read it Last article , have an understanding of the core components of the Web framework.

Directory structure of source code

Taking v1.7.4 as an example, the source directory structure of Gin is shown in the figure below

+---.github
|       ISSUE_TEMPLATE.md
|       PULL_REQUEST_TEMPLATE.md
|       
+---binding
|       binding.go
|       binding_msgpack_test.go
|       binding_nomsgpack.go
|       binding_test.go
|       default_validator.go
|       default_validator_test.go
|       form.go
|       form_mapping.go
|       form_mapping_benchmark_test.go
|       form_mapping_test.go
|       header.go
|       json.go
|       json_test.go
|       msgpack.go
|       msgpack_test.go
|       multipart_form_mapping.go
|       multipart_form_mapping_test.go
|       protobuf.go
|       query.go
|       uri.go
|       validate_test.go
|       xml.go
|       xml_test.go
|       yaml.go
|       yaml_test.go
|       
+---examples
|       README.md
|       
+---ginS
|       gins.go
|       README.md
|       
+---internal
|   +---bytesconv
|   |       bytesconv.go
|   |       bytesconv_test.go
|   |       
|   |---json
|           json.go
|           jsoniter.go
|           
+---render
|       data.go
|       html.go
|       json.go
|       msgpack.go
|       protobuf.go
|       reader.go
|       reader_test.go
|       redirect.go
|       render.go
|       render_msgpack_test.go
|       render_test.go
|       text.go
|       xml.go
|       yaml.go
|       
|---testdata
|    +---certificate
|    |       cert.pem
|    |       key.pem
|    |       
|    +---protoexample
|    |       test.pb.go
|    |       test.proto
|    |       
|    |---template
|            hello.tmpl
|            raw.tmpl
|   .gitignore
|   .travis.yml
|   auth.go
|   AUTHORS.md
|   auth_test.go
|   BENCHMARKS.md
|   benchmarks_test.go
|   CHANGELOG.md
|   codecov.yml
|   CODE_OF_CONDUCT.md
|   context.go
|   context_appengine.go
|   context_test.go
|   CONTRIBUTING.md
|   debug.go
|   debug_test.go
|   deprecated.go
|   deprecated_test.go
|   doc.go
|   errors.go
|   errors_1.13_test.go
|   errors_test.go
|   fs.go
|   gin.go
|   gin_integration_test.go
|   gin_test.go
|   githubapi_test.go
|   go.mod
|   go.sum
|   LICENSE
|   logger.go
|   logger_test.go
|   Makefile
|   middleware_test.go
|   mode.go
|   mode_test.go
|   path.go
|   path_test.go
|   README.md
|   recovery.go
|   recovery_test.go
|   response_writer.go
|   response_writer_test.go
|   result.txt
|   routergroup.go
|   routergroup_test.go
|   routes_test.go
|   test_helpers.go
|   tree.go
|   tree_test.go
|   utils.go
|   utils_test.go
|   version.go

use cloc The tool scans the source code, and the total number of lines of code is less than 1.2W.

http://cloc.sourceforge.net v 1.64  T=0.91 s (97.2 files/s, 18596.7 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Go                              84           2537           2360          11796
make                             1              8              0             63
YAML                             2              8              0             47
Protocol Buffers                 1              2              0             10
-------------------------------------------------------------------------------
SUM:                            88           2555           2360          11916
-------------------------------------------------------------------------------

Description of directory structure

catalogueeffect
root directoryThe core of the whole Web framework is under the root directory, including server, router and middleware.
bindingTurn the data of http request into the self-defined structure variables in Go. You don't have to parse the parameters in http request one by one to reduce the development workload.
examplesUsing some code examples of Gin, there is only one README.md in this directory. For specific examples, see Gin Examples In this separate Repo.
ginSRelying on sync.Once to implement a singleton version of gin.Engine, it seems useless at present, and the author In communication , you can ignore it first.
internalFunction used internally by Gin. The standard library encoding/json and the third-party encoding/json are referenced in the sub package json json-iterator , the conversion between string and byte slice of zero memory allocation version is realized in the bytesconv sub package.
renderIt supports rendering XML, JSON, YAML, ProtoBuf and HTML data and returning them to the response format recognized by the front end.
testdataSome test data.

Root directory source code description

Sort according to the importance of source code

Source codeeffect
gin.goThe definition and method implementation of gin.Engine, and the entry program of Gin
context.go
context_appengine.go
Definition and method implementation of gin.Context
response_write.goProcessing of response data
routergroup.go
tree.go
The specific implementation of routing, in which tree.go comes from httprouter , based on radius tree, without reflection
auth.go
recovery.go
logger.go
Middleware: auth.go is http authentication middleware, recovery.go is program panic capture middleware, and logger.go is log middleware
mode.goThe initialization management of Gin service startup mode supports three modes: debug, release and test
debug.gomode.go when the debug mode is enabled, print some debugging information
fs.goThe auxiliary code related to Static file access is mainly used for the Static method of routergroup.go
path.goAuxiliary function for processing url path
utils.goSome auxiliary functions
version.goRecord the version information of the current Gin framework
deprecated.goThere is only one BindWith method to be deleted in the future. Methods in deprecated.go should not be used in programs.
doc.goAt present, it has no practical use and can be ignored
xx__test.goTest procedure

In subsequent articles, each source code file of Gin will be analyzed in detail. ( https://mp.weixin.qq.com/s/An...)

Article open source address

https://github.com/jincheng9/...
There will be a series to share all aspects of Go Web development.

Welcome to the official account: coding step up, learn more Go knowledge.

Tags: Go Web Development Back-end

Posted on Fri, 26 Nov 2021 23:04:27 -0500 by Mystis