package main import ( "database/sql" "encoding/xml" "fmt" _ "github.com/go-sql-driver/mysql" "io/ioutil" "os" ) type Conf struct { XMLName xml.Name `xml:"Configuration"` Version string `xml:"Version,attr"` Database DatabaseConfig `xml:"Database"` Description string `xml:",innerxml"` } type DatabaseConfig struct { XMLName xml.Name `xml:"Database"` DBtype string `xml:"DBtype"` DBhost string `xml:"DBhost"` DBport string `xml:"DBport"` DBname string `xml:"DBname"` Protocol string `xml:"Protocol"` DBuser string `xml:"DBuser"` DBpwd string `xml:"DBpwd"` DBcharset string `xml:"DBcharset"` Testsql string `xml:"Testsql"` } func checkDBerr(err error) { if err != nil { fmt.Println("DBerror:", err) } } func checkIOerr(err error) { if err != nil { fmt.Println("IOerror:", err) } } func main() { //判断配置文件是否存在,如果不存在则提示用户输入配置并创建文件,如果存在则直接读取 configFile, err := os.Open("src/conf.xml") defer configFile.Close() if err != nil { if false == os.IsExist(err) { config := Conf{} fmt.Println("Config file not exists,please input the config:") fmt.Println("Input the database type:") fmt.Scanf("%s", &config.Database.DBtype) if "mysql" == config.Database.DBtype { fmt.Println("The database type is:", config.Database.DBtype) fmt.Println("Input the database host:") fmt.Scanf("%s", &config.Database.DBhost) fmt.Println("Input the database port:") fmt.Scanf("%s", &config.Database.DBport) fmt.Println("Input the database name:") fmt.Scanf("%s", &config.Database.DBname) fmt.Println("Input the network protocol:") fmt.Scanf("%s", &config.Database.Protocol) fmt.Println("Input the database user:") fmt.Scanf("%s", &config.Database.DBuser) fmt.Println("Input the database password:") fmt.Scanf("%s", &config.Database.DBpwd) fmt.Println("Input the database character set:") fmt.Scanf("%s", &config.Database.DBcharset) config.Database.Testsql = "select welcomeinfo from welcomeinfo" config.Version="0.0.0" dbConnStr := config.Database.DBuser + ":" + config.Database.DBpwd + "@" + config.Database.Protocol + "(" + config.Database.DBhost + ":" + config.Database.DBport + ")/" + config.Database.DBname + "?charset=" + config.Database.DBcharset fmt.Println("Database type is:", config.Database.DBtype) fmt.Println("Database connection string is:", dbConnStr) fmt.Println("Datebase test SQL:", config.Database.Testsql) fmt.Println("Testing the connection of database...") db, err := sql.Open("mysql", dbConnStr) defer db.Close() checkDBerr(err) rows, err := db.Query(config.Database.Testsql) checkDBerr(err) if rows.Next() { var welcomeinfo string rows.Scan(&welcomeinfo) fmt.Println("Connect to database succeeded:", welcomeinfo) fmt.Println("Generating config file...") outXML,err:=xml.MarshalIndent(config," "," ") checkIOerr(err) //os.Stdout.Write(outXML) configFile,err:=os.Create("src/conf.xml") defer configFile.Close() checkIOerr(err) configFile.Write(outXML) fmt.Println("Config file generated!") } else { fmt.Println("TestSQL failed!") } } else { fmt.Println("Sorry,database type not supported:", config.Database.DBtype) } } else { checkIOerr(err) } } else { //如果配置文件存在则直接读取配置并连接到数据库 fmt.Println("Loading config file...") data, err := ioutil.ReadAll(configFile) checkIOerr(err) config := Conf{} err = xml.Unmarshal(data, &config) checkIOerr(err) if "mysql" == config.Database.DBtype { dbConnStr := config.Database.DBuser + ":" + config.Database.DBpwd + "@" + config.Database.Protocol + "(" + config.Database.DBhost + ":" + config.Database.DBport + ")/" + config.Database.DBname + "?charset=" + config.Database.DBcharset fmt.Println("Database type is:", config.Database.DBtype) fmt.Println("Database connection string is:", dbConnStr) fmt.Println("Datebase test SQL:", config.Database.Testsql) fmt.Println("Testing the connection of database...") db, err := sql.Open("mysql", dbConnStr) defer db.Close() checkDBerr(err) rows, err := db.Query(config.Database.Testsql) checkDBerr(err) if rows.Next() { var welcomeinfo string rows.Scan(&welcomeinfo) fmt.Println("Connect to database succeeded:", welcomeinfo) } else { fmt.Println("TestSQL failed!") } } else { fmt.Println("Sorry,database type not supported:", config.Database.DBtype) } fmt.Println("Finish loading config file.") } }