目录结构
```
proj
│ go.mod
│ main.go
│ go.sum
│ user.db
│
└─models
dao.go
```
proj\models\dao.go
```
package models
import (
"fmt"
"github.com/go-xorm/xorm"
_ "github.com/mattn/go-sqlite3"
"time"
)
var (
baseTimestamp time.Time
x *xorm.Engine
)
func CallFuncFromMoles() {
fmt.Println(baseTimestamp)
fmt.Println(x)
}
func init() {
fmt.Println("models init")
baseTimestamp = time.Now()
fmt.Println(baseTimestamp)
x, err := xorm.NewEngine("sqlite3", "user.db")
fmt.Println(x, err)
}
```
proj\main.go
```
package main
import (
"proj/models"
"fmt"
)
func main(){
fmt.Println("now in main CallFuncFromMoles")
models.CallFuncFromMoles()
}
```
go run main.go
```
ef20 UUID:0xf4f2e0 VARBINARY:0xf4f2e0 VARCHAR:0xf4f2e0 VERSION:0xf4eea0 YEAR:0xf4f2e0] <nil> map[] {{0 0} 0 0 0 0} 0xc0000ae128} <nil>
now in main CallFuncFromMoles
2021-03-17 21:09:08.1911855 +0800 CST m=+0.006000301
<nil>
```
baseTimestamp init和 CallFuncFromMoles都 有值
x 只在init 有值,CallFuncFromMoles为nil
有疑问加站长微信联系(非本文作者)