Hello, I’m a Rocket
This is the second day of my participation in the Gwen Challenge. For more details: the last Gwen Challenge in 2021
The introduction
- Add support to generate command, service, controller, view, source code in the project
- Making portal
- Like iron son to point a star
- This is where the old guys who didn’t read the last article enter the portal
- The TPL template is under CMD/TPL
1. Make :command Generates commands
The code in the CMD/make -command. Go
Core method: util.CommandCreate
3. Run the text/template command to write the contents of the file. Func CommandCreate(use, fileName, cmdName, cmdParent string) error { cmdFilePath := fmt.Sprintf("%s.go", fileName) cmdFile, err := tool.CreateFile(cmdFilePath) if err ! = nil { return err } defer cmdFile.Close() c := commandStruct{ Use: use, CmdName: cmdName, CmdParent: cmdParent, } commandTemplate := template.Must(template.New("sub").Parse(tpl.AddCommandTemplate())) err = commandTemplate.Execute(cmdFile, c) if err ! = nil { return err } return nil }Copy the code
2, make:service generate service layer
Code in the CMD/make – service. Go
Core code: util.servicecreate
func ServiceCreate(model, fileName, path string) (string, error) { ... Model s := serviceStruct{Package: Package, // TPL {{.Package}} ModelImport: ModelImport, ModelName: ModelName, ModelStruct: ModelStruct, // Similar to models.sysmenu} serviceTemplate := template.Must(template.new ("ser").parse (tpl.serviceTemplate ())) err = serviceTemplate.Execute(File, s) if err ! = nil { return path + fileName, err } return path + fileName, nil }Copy the code
3, make:controller generates the controller layer
Code in the CMD/make – controller. Go
Core code: util.controllerCreate
func ControllerCreate(path, name, model, service string) (string, error) { ... Omit s := controllerStruct{Package: Package, ControllerName: ControllerName, ViewName: ViewName, Name: name, ModelImport: ModelImport, ModelStruct: ModelStruct, ServiceImport: serviceImport, ServicePackage: servicePackage, ServiceName: serviceName, } controllerTemplate :=template.Must(template.New("ser").Parse(tpl.ControllerTemplate())) err = controllerTemplate.Execute(File, s) if err ! = nil { return fileName, err } return fileName, nil }Copy the code
4, make:view generate view
Code in the CMD/make – view. Go
Core code:
func ViewCreate(path, desc string) error { ... Omit v := viewStruct{Default: path, Desc: Desc,} controllerTemplate := template.Must(template.new ("view").delims ("<! --{", "}-->").Parse(tpl.ViewTemplate())) err = controllerTemplate.Execute(File, v) if err ! = nil { return err } return nil }Copy the code
5. Follow-up plan
The next article is ready to integrate CMD command, 10 minutes to quickly implement a function (including pages, permissions, buttons, pop-ups, etc.)
6. Series of articles
- Serialized a Golang environment build
- Serial II installation Gin
- Serial three defines the directory structure
- Serial four builds case API1
- Serial five builds case API2
- Serial six access Swagger interface document
- Serial seven Log components
- Serial eight gracefully restarts and stops
- Serial Makefile build
- Serial Cron timing mission
- Serial build command line tools
- Create a dedicated Cache(First Day)
- Create your own Cache in 3 days (Second Day)
- Create a dedicated Cache(Third Day)
- Gin Framework practice [GO-GIN_API]2.0
- Gin Framework Practices [GO-GIN_API]2.0 tool section