Installation dependencies
There are two more dependencies needed to be installed here.gorm
、viper
, For specific purposes, please visit their official website (Gorm official website address Viper Github address)
Initialize configuration
Open the terminal in the root directory to execute:
go mod init project name (for example: go mod init demo)
Installation dependencies
go get /jinzhu/gorm or go get -u /gorm
go get -u /driver/mysql
go get /spf13/viper
Create a file
Create a yml file ()
#server:
# post: 8888
datasource:
driverName: mysql
host: # Port address
port: # port number
database: # database name
username: # Account
password: # Password
charset: utf8
Create a common package in the directory, create a file under the package, and write an initialization database internally. It is recommended to split it out. This splicing method is conducive to the project to quickly change configurations in different environments.
package common import ( "fmt" // See clearly the introduction of packages, there are differences in methods for the differences in packages "/jinzhu/gorm" "/spf13/viper" ) // Declare in capitalization (can be accessed globally)var DB * // Create a database connection poolfunc InitDB() * { driverName := ("") host := ("") port := ("") database := ("") username := ("") password := ("") charset := ("") args := ("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=true", username, password, host, port, database, charset) db, err := (driverName, args) if err != nil { ("fail err mysql", ()) } // gorm automatically creates tables, and models that need to be placed in the model layer, such as User{} (User{}) // Assign value otherwise the pointer will be empty DB = db return db } // Get example of DBfunc GetDB() * { return DB }
Main function execution
Create a main file in the root directory(only)
, In this environment, we need to do an operation to initialize the configuration file. We need to load this config first in main, and then initialize the database. The order cannot be wrong, because the connection information of the database is in the yml file, so don’t make the order wrong.
package main import ( "/gin-gonic/gin" _ "/go-sql-driver/mysql" "/spf13/viper" "go-gin-vue/common" "os" ) func main() { InitConfig() // Try to connect to the database db := () // Delayed shutdown defer () r := () // Loading the route r = CollectRouter(r) // The listening port // You can define the server port in yml and use the port defined in the yml file // port := ("") if port != "" { panic((":" + port)) } (":9000") // Start the service on test port 9000} // Initialize the config filefunc InitConfig() { workDir, _ := () // Read file name ("application") // Read file type ("yml") // Read path (workDir + "/config") err := () if err != nil { panic("") } }
This is the introduction to this article about the brief analysis of Go initialization database method. For more relevant Go initialization database content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!