Skip to main content

Preparations

This article begins recording as of September 2023. If there are updates to the official documentation that cause the links in this article to become invalid or if there are discrepancies or errors, the official documentation shall prevail.

Prerequisites

Go development environment installed, with GOROOT and GOPATH environment variables configured

Familiarity with basic Go syntax and usage

GoFrame documentation: https://goframe.org/

GoFrame2 video tutorial: https://www.bilibili.com/video/BV1Uu4y1u7kX/

The learning process is mainly based on the official documentation. The content of this article is taken from the official documentation.

This stage only introduces the web development part; the microservices part will be introduced in a separate article if there is an opportunity.

Installing the Framework Tool

https://github.com/gogf/gf/releases

Download the corresponding package and install it. It is recommended to install it in the GOROOT's bin directory.

Use the following command to check if the installation was successful:

gf -v

Project Initialization

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn
# If you have already set these, you can skip the above two lines

gf init gf_demo -u # If you have previously created a project and do not need to create the latest version, omit the -u

Common proxy addresses:

  • https://goproxy.cn
  • https://goproxy.io
  • https://mirrors.aliyun.com/goproxy/

Project Startup

Enter the directory where the main.go file of the project is located and run the following command:

gf run main.go

After starting successfully, enter http://127.0.0.1:8000/hello in the browser to see the result.

Framework Design

The content on framework design is somewhat abstract and tends to be theoretical, making it difficult for beginners to understand. Therefore, the other content in this section can be studied later. However, it is necessary to understand some basic knowledge, such as MVC and 3-Tier Architecture. For more details, see the documentation on Code Layered Design. It is not necessary to fully understand it, just get a general idea.

Project Directory Structure

/
├── api Definitions of request interface input/output data structures
├── hack Project development tools and scripts
├── internal Directory for business logic and core code
│ ├── cmd Directory for entry commands and other command tools
│ ├── consts Directory for constant definitions
│ ├── controller Controller directory for receiving and parsing user requests
│ ├── dao Data access object directory for interacting with the underlying database
│ ├── logic Directory for core business logic code
│ ├── model Data structure management module, managing data entity objects, and input/output data structure definitions
│ │ ├── do Business model to instance model conversion for data operations, maintained by tools and not manually modified
│ │ └── entity Data model is a one-to-one relationship between model and data collection, maintained by tools and not manually modified
│ └── service Business interface definition layer, with specific interface implementations injected in logic
├── manifest Files for program compilation, deployment, operation, and configuration
├── resource Static resource files
├── utility
├── go.mod
└── main.go Program entry file

For more detailed introductions to the project directory and request layer transfer, see the documentation on Project Directory Design.