1 回答

TA貢獻1856條經驗 獲得超17個贊
您可以擁有一個名為 MainController 的文件,您可以在其中創建訪問數據庫、會話、配置文件等的功能。從那里你真正需要做的就是說這樣的話(例如從其他控制器繼承東西):
//MyController.go
type App struct {
MainController
}
func (c Application) MyControllerFunc() returnTypeHere(http.Response for example) {
//c.getDatabaseName is function from MainController that reads information from some plain text file or json file or similar
someInfoFromConfigFile = c.getDatabaseName()
var str []string
str = append(str, someInfoFromConfigFile)
//RenderJson is function that render http response as json (Content type plain/json)
return c.RenderJson(str)
}
但是如果你需要 mvc,我建議你使用一些框架(例如 Revel)。我一直在使用它。如果您愿意,它會為您提供基本的 mvc 功能,而所有其他內容完全取決于您。您將業務邏輯保存在一些助手中,模型文件中的模型等。
您可以在此處查看 Revel 主控制器結構: https ://github.com/revel/revel/blob/master/controller.go
添加回答
舉報