Code
Congratulations! Now you could run the project and see the result from your browser.
There are still something to do before we adding any features to the project. Don’t worry, we will finish it very quickly.
Import database driver
GoFrame has many supported databases. But to use them, you need to import drivers into your project first.
Install database driver:
go get -u github.com/gogf/gf/contrib/drivers/mysql/v2
Open main.go
in the project folder with your favorite editor. Add the following import statement:
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
Full main.go
file
package main
import (
_ "bootcamp/internal/packed"
"github.com/gogf/gf/v2/os/gctx"
"bootcamp/internal/cmd"
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
)
func main() {
cmd.Main.Run(gctx.GetInitCtx())
}
Then it is finished, you don’t need to worry about the driver any more.
Generate Database Access Object(DAO)
As we mentioned before, gf
has many powerful code generation functions. Now we will use gf gen dao
, which was configured before, to generate the DAO related code.
Depend on your previous settings, you may use this command in your docker container or your local machine, and it will generate related code automatically.
$ gf gen dao
generated: internal\dao\internal\messages.go
generated: internal\dao\internal\users.go
generated: internal\model\do\messages.go
generated: internal\model\do\users.go
generated: internal\model\entity\messages.go
generated: internal\model\entity\users.go
done!
gf gen dao
again to refresh the DAO related code.