0

I use xorm to connect to my mysql database,but when my mysql doesn't start,xorm cann't print error message

package main

import (
    "fmt"

    _ "github.com/go-sql-driver/mysql"
    "xorm.io/xorm"
)

var engine *xorm.Engine

func main() {
    var err error
    engine, err = xorm.NewEngine("mysql", "root:1234567@/blog?charset=utf8mb4")
    if err != nil {
        fmt.Println(err.Error())//can't print
        return
    }
}

enter image description here

Symfonying
  • 13
  • 3

1 Answers1

0

Use the Ping method to check whether the database is alive

if err := engine.Ping(); err != nil {
    panic(err)
}

Ping method

kaushik
  • 2,308
  • 6
  • 35
  • 50
  • thanks,the problem is solved.what’s the usage of err in my code,or when i use it – Symfonying May 27 '22 at 02:00
  • I've updated the code on how to use the error. If my answer solved your issue, would you be kind enough to accept my answer. – kaushik May 27 '22 at 05:13