Found a good golang framework, the documentation is very detailed, with its own code production dao tool, which can quickly carry out project development, and the community is also very active

foreword


The original link of this article is:
https://blog.csdn.net/freewebsys/article/details/129698017

Not to be reproduced without permission of the blogger.
The blogger’s CSDN address is: https://blog.csdn.net/freewebsys
The blogger’s nugget address is: https://juejin.cn/user/585379920479288
The blogger’s Zhihu address is: https://www.zhihu. com/people/freewebsystem

1. Introduction to the goframe framework

Project address: https://goframe.org/

GoFrame is a modular, low-coupling design development framework that includes common basic components and development tools, and can be used as a complete business project framework or as an independent component library. The quick start chapter we provide for you mainly introduces the basic introduction and use of the framework with a complete business project. For the use of the independent component library, you can view the introduction to the independent component chapter.

github project address:

https://github.com/gogf/gf

cd $GOPATH
mkdir -p github.com/gogf/
cd github.com/gogf/
git clone  https://github.com/gogf/gf.git
cd cmd/gf
go install


Check out the command line:

gf
USAGE
    gf COMMAND [OPTION]

COMMAND
    up         upgrade GoFrame version/tool to latest one in current project
    env        show current Golang environment variables
    fix        auto fixing codes after upgrading to new GoFrame version
    run        running go codes with hot-compiled-like feature
    gen        automatically generate go files for dao/do/entity/pb/pbentity
    tpl        template parsing and building commands
    init       create and initialize an empty GoFrame project
    pack       packing any file/directory to a resource file, or a go file
    build      cross-building go project for lots of platforms
    docker     build docker image for current GoFrame project
    install    install gf binary to system (might need root/admin permission)
    version    show version information of current binary

OPTION
    -y, --yes       all yes for all command without prompt ask
    -v, --version   show version information of current binary
    -d, --debug     show internal detailed debugging information
    -h, --help      more information about this command

ADDITIONAL
    Use "gf COMMAND -h" for details about a command.
    

Then create the project:

gf init gf-demo
cd gf-demo 
gf run main.go 

It will start the web port at 8000, both interface and api.

2023-03-21 15:28:00.719 [INFO] swagger ui is serving at address: http://127.0.0.1:8000/swagger/
2023-03-21 15:28:00.719 [INFO] openapi specification is serving at address: http://127.0.0.1:8000/api.json

  ADDRESS | METHOD |   ROUTE    |                             HANDLER                             |           MIDDLEWARE             
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | ALL    | /*         | github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing | GLOBAL MIDDLEWARE                
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | ALL    | /api.json  | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec           |                                  
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | GET    | /hello     | gf-demo/internal/controller/hello.(*Controller).Hello           | ghttp.MiddlewareHandlerResponse  
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | ALL    | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI             | HOOK_BEFORE_SERVE                
----------|--------|------------|-----------------------------------------------------------------|----------------------------------

2. Create Dao code

Engineering project path:
https://goframe.org/pages/viewpage.action?pageId=30740166

/
├── api
├── hack
├── internal
│   ├── cmd
│   ├── consts
│   ├── controller
│   ├── dao
│   ├── logic
│   ├── model
│   |   ├── do
│   │   └── entity
│   └── service
├── manifest
├── resource
├── utility
├── go.mod
└── main.go 

The tool configures the yaml in the hack and then you can:

Configure the database address. Then execute the generated code:

make dao
generated: internal/dao/user_info.go
generated: internal/dao/internal/user_info.go
generated: internal/model/do/user_info.go
generated: internal/model/entity/user_info.go
done!

Mainly the dao code of user_info is generated:


// UserInfoDao is the data access object for table user_info.
type UserInfoDao struct {
    
    
	table   string          // table is the underlying table name of the DAO.
	group   string          // group is the database configuration group name of current DAO.
	columns UserInfoColumns // columns contains all the column names of Table for convenient usage.
}

// UserInfoColumns defines and stores column names for table user_info.
type UserInfoColumns struct {
    
    
	Id       string // 主键Id
	Name     string // 用户名
	Password string // 密码
	Status   string // 状态
	Type     string // 类型
}

// userInfoColumns holds the columns for table user_info.
var userInfoColumns = UserInfoColumns{
    
    
	Id:       "id",
	Name:     "name",
	Password: "password",
	Status:   "status",
	Type:     "type",
}

// NewUserInfoDao creates and returns a new DAO object for table data access.
func NewUserInfoDao() *UserInfoDao {
    
    
	return &UserInfoDao{
    
    
		group:   "default",
		table:   "user_info",
		columns: userInfoColumns,
	}
}

// DB retrieves and returns the underlying raw database management object of current DAO.
func (dao *UserInfoDao) DB() gdb.DB {
    
    
	return g.DB(dao.group)
}

// Table returns the table name of current dao.
func (dao *UserInfoDao) Table() string {
    
    
	return dao.table
}

// Columns returns all column names of current dao.
func (dao *UserInfoDao) Columns() UserInfoColumns {
    
    
	return dao.columns
}

// Group returns the configuration group name of database of current dao.
func (dao *UserInfoDao) Group() string {
    
    
	return dao.group
}

// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation.
func (dao *UserInfoDao) Ctx(ctx context.Context) *gdb.Model {
    
    
	return dao.DB().Model(dao.table).Safe().Ctx(ctx)
}

// Transaction wraps the transaction logic using function f.
// It rollbacks the transaction and returns the error from function f if it returns non-nil error.
// It commits the transaction and returns nil if function f returns nil.
//
// Note that, you should not Commit or Rollback the transaction in function f
// as it is automatically handled by this function.
func (dao *UserInfoDao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
    
    
	return dao.Ctx(ctx).Transaction(ctx, f)
}

3. Method of testing Dao

Then you can generate the dao template, and then call it through the controller.

Refer to the api documentation of orm, and simply test it, it is quite simple.

	var (
		m = dao.UserInfo.Ctx(ctx)
	)
	userInfo := entity.UserInfo{
    
    
		Name:     "zhangsan",
		Password: "123456",
		Status:   1,
	}

	lastId, err := m.Data(userInfo).InsertAndGetId()
	if err == nil {
    
    
		g.Log().Printf(ctx, "########## insertId %d #######", lastId)
	}

	count, err := m.Count()
	if err == nil {
    
    
		g.Log().Printf(ctx, "########## count: %d #######", count)
	}

	userList := []entity.UserInfo{
    
    }

	err = m.Scan(&userList)

	g.Log().Printf(ctx, "########## userList: %v #######", userList)

4. Summary

The goframe framework is still a very good web framework, and at the same time, it also uses the tools needed for development as much as possible.
The class library has been enriched, and it is relatively easy to use. It is a full-stack MVC framework.
A similar product is beego, which is equivalent to a beego plus version. Many people are already using it.
There are also a lot of people using it.

The original link of this article is:
https://blog.csdn.net/freewebsys/article/details/129698017

insert image description here

Guess you like

Origin blog.csdn.net/freewebsys/article/details/129698017