Home >Backend Development >Golang >golang directory settings

golang directory settings

WBOY
WBOYOriginal
2023-05-22 16:08:07708browse

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

In the above structure, we divide it into cmd, pkg, internal according to the functional modules of the project. Three parts:

  • cmd Directory stores command line tools that can be run directly, such as server program server and client program client. The
  • pkg directory stores the business logic code of the project, which is divided according to functional modules, such as the user module and the util module.
  • 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 the pkg and internal directories look similar, their difference is that pkg## The code in the # directory can be used by external packages, while the code in the internal directory 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

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

cmd, pkg, and vendor according to the code type. Part: The

  • cmd directory contains the entry file for the executable program, such as main.go. The
  • pkg directory is divided according to code type. For example, HTTP-related codes are placed in the http directory, and database-related codes are placed in the database directory 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

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

models, views, and controllers based on the MVC pattern. Part:

  • models The 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!

Statement:
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
Previous article:win install golangNext article:win install golang