我正在尝试在学习Go时尝试理解MVC架构,并且我试图从控制器中更改模型中的值.
代码现在创建一个只保存字符串的模型,视图显示终端上的字符串,但控制器无法更改它(它获得用户输入没有任何问题).
现在我在终端上的文字是这样的:
Hello World!
asdf //my input
Hello World!
Run Code Online (Sandbox Code Playgroud)
我想得到的输出将是这样的:
Hello World!
asdf //my input
asdf
Run Code Online (Sandbox Code Playgroud)
这是我的文件:
model.go
package models
type IndexModel struct {
Text string
}
func (m *IndexModel) InitModel() string {
return m.Text
}
func (m *IndexModel) SetText(newText string) {
m.Text = newText
}
Run Code Online (Sandbox Code Playgroud)
view.go
package views
import "github.com/jufracaqui/mvc_template/app/models"
type IndexView struct {
Model models.IndexModel
}
func (v IndexView) Output() string {
return v.Model.Text
}
Run Code Online (Sandbox Code Playgroud)
controller.go
package controllers
import "github.com/jufracaqui/mvc_template/app/models"
type IndexController struct {
Model models.IndexModel
}
func (c IndexController) ChangeText(userInput string) {
c.Model.SetText(userInput)
}
Run Code Online (Sandbox Code Playgroud)
main.go:
package main
import (
"bufio"
"os"
"fmt"
"github.com/jufracaqui/mvc_template/app/controllers"
"github.com/jufracaqui/mvc_template/app/models"
"github.com/jufracaqui/mvc_template/app/views"
)
func main() {
handleIndex()
}
func handleIndex() {
model := models.IndexModel{
"Hello World!",
}
controller := controllers.IndexController{
model,
}
viewIndex := views.IndexView{
model,
}
fmt.Println(viewIndex.Model.Text)
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
controller.ChangeText(text)
fmt.Println(viewIndex.Model.Text)
}
Run Code Online (Sandbox Code Playgroud)
编辑: @JimB回答后我的代码如何结束:
model.go:
package models
type IndexModel struct {
Text string
}
func (m *IndexModel) InitModel() string {
return m.Text
}
Run Code Online (Sandbox Code Playgroud)
view.go:
package views
import "github.com/jufracaqui/mvc_template/app/models"
type IndexView struct {
Model *models.IndexModel
}
func (v IndexView) Output() string {
return v.Model.Text
}
Run Code Online (Sandbox Code Playgroud)
controller.go:
package controllers
import "github.com/jufracaqui/mvc_template/app/models"
type IndexController struct {
Model *models.IndexModel
}
func (c IndexController) ChangeText(userInput string) {
c.Model.Text = userImput
}
Run Code Online (Sandbox Code Playgroud)
main.go:
package main
import (
"bufio"
"os"
"fmt"
"github.com/jufracaqui/mvc_template/app/controllers"
"github.com/jufracaqui/mvc_template/app/models"
"github.com/jufracaqui/mvc_template/app/views"
)
func main() {
handleIndex()
}
func handleIndex() {
model := models.IndexModel{
"Hello World!",
}
m := &model
controller := controllers.IndexController{
m,
}
viewIndex := views.IndexView{
m,
}
fmt.Println(viewIndex.Model.Text)
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
controller.ChangeText(text)
fmt.Println(viewIndex.Model.Text)
}
Run Code Online (Sandbox Code Playgroud)
IndexController.ChangeText需要一个指针接收器,或者IndexController.Model需要一个指针.您正在调用SetText该SetText值的副本.
如果你期望事物是可变的,那么始终如一地使用指向结构的指针要容易得多,并且当你真正需要时,使显式的struct值成为异常.