beego 导入一个普通的包都会执行init方法,如果是struct就不会执行

时间:2021-07-04 15:29:25

beego  导入一个普通的包都会执行init方法,如果是struct就不会执行

default.go

package controllers

import (
"beego-test/models"
"beego-test/service"
"github.com/astaxie/beego"
) type MainController struct {
beego.Controller
} func (c *MainController) Get() {
c.Data["Website"] = "beego.me"
c.Data["Email"] = "astaxie@gmail.com"
service.Print()
models.Mmodel()
new(service.Service2).Print2()
c.TplName = "index.tpl"
}

models.go

package models

import (
"fmt"
) func init() {
fmt.Println("...initMmodel...")
} func Mmodel() {
fmt.Println("...PrintMmodel...")
}

service.go

package service

import (
"fmt"
) func init() {
fmt.Println("...init...")
} func Print() {
fmt.Println("...Print...")
}

service1.go

package service

import (
"fmt"
) func init() {
fmt.Println("...init1...")
} func Print1() {
fmt.Println("...Print1...")
}

service2.go

package service

import (
"fmt"
) type Service2 struct {
} func (this *Service2) init() {
fmt.Println("...init.service2..")
} func (this *Service2) Print2() {
fmt.Println("...Print.service2..")
}