golang directory settings

WBOY
Release: 2023-05-22 16:08:07
Original
562 people have browsed it

Golang is an open source programming language that is widely used for network service development, high-concurrency applications, and cloud services. When writing a project in golang, a good directory structure can make the project clearer and easier to maintain. Today we will discuss the settings of the golang directory structure.

  1. Dividing according to functions

Dividing projects according to functional modules is a common directory structure setting in golang. The specific method is to create multiple subdirectories in the project root directory, each subdirectory corresponding to a functional module. For example, the following example:

myproject/ ├── cmd/ │ ├── server/ │ │ ├── main.go │ ├── client/ │ │ ├── main.go ├── pkg/ │ ├── user/ │ │ ├── user.go │ ├── util/ │ │ ├── util.go ├── internal/ │ ├── auth/ │ │ ├── auth.go │ ├── db/ │ │ ├── db.go ├── vendor/ ├── go.mod ├── go.sum
Copy after login

In the above structure, we divide it intocmd,pkg,internalaccording to the functional modules of the project. Three parts:

  • cmdDirectory stores command line tools that can be run directly, such as server programserverand client programclient. The
  • pkgdirectory stores the business logic code of the project, which is divided according to functional modules, such as theusermodule and theutilmodule.
  • internalThe internal code of the project is stored in the directory, which is only used in the project and will not be used by external packages.

It is worth noting that although the functions of thepkgandinternaldirectories look similar, their difference is thatpkg## The code in the # directory can be used by external packages, while the code in theinternaldirectory can only be used in this project.

    Divide according to code type
Dividing code according to type is another common way to set up the golang directory structure. The specific method is to create multiple subdirectories in the project root directory, each subdirectory corresponding to a code type. For example, in the following example:

myproject/ ├── cmd/ │ ├── main.go ├── pkg/ │ ├── http/ │ │ ├── server.go │ │ ├── router.go │ ├── database/ │ │ ├── db.go │ ├── log/ │ │ ├── log.go ├── vendor/ ├── go.mod ├── go.sum
Copy after login

In the above structure, we divide it into three types:

cmd,pkg, andvendoraccording to the code type. Part: The

  • cmddirectory contains the entry file for the executable program, such asmain.go. The
  • pkgdirectory is divided according to code type. For example, HTTP-related codes are placed in thehttpdirectory, and database-related codes are placed in thedatabasedirectory and so on.
  • vendorThe directory stores the third-party packages that the project depends on.
Compared with the method of dividing by function, this method of dividing by code type is more flexible, but it may lead to a deeper directory structure and require more time to find the location of the code.

    Divide according to the MVC pattern
Most web frameworks adopt the MVC (Model-View-Controller) pattern, and it is also very easy to divide the program according to this pattern. A common way to set up the golang directory structure. For example, the following example:

myproject/ ├── cmd/ │ ├── main.go ├── pkg/ │ ├── models/ │ │ ├── user.go │ ├── views/ │ │ ├── index.gohtml │ ├── controllers/ │ │ ├── user.go ├── vendor/ ├── go.mod ├── go.sum
Copy after login
In the above structure, we divide it into three parts:

models,views, andcontrollersbased on the MVC pattern. Part:

  • modelsThe model layer code is stored in the directory, usually the code that deals with the database.
  • viewsThe view layer code is stored in the directory, usually web page templates, etc.
  • controllersThe directory stores the controller layer code, which is responsible for connecting the model layer and the view layer.
This way of dividing according to the MVC pattern can make the code more organized and easier to maintain.

To sum up, there are many ways to set the directory in golang, and different ways are suitable for different projects. We can choose the corresponding directory setting method according to our own needs.

The above is the detailed content of golang directory settings. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!