Setter 在模型中不起作用

Setter not working in model

我正在编写这段代码,试图在学习 Go 的同时理解 MVC 架构,但我一直在尝试从控制器更改模型中的值。

现在的代码创建了一个只包含一个字符串的模型,视图在终端上显示该字符串,但控制器无法更改它(它可以毫无问题地获取用户输入)。

现在我在终端中得到的文本是这样的:

Hello World!
asdf //my input
Hello World!

我想得到的输出是这样的:

Hello World!
asdf //my input
asdf

这是我的文件:

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
}

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
}

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)
}

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)
}

编辑: @JimB 回答后我的代码如何结束:

model.go:

package models

type IndexModel struct {
    Text string
}

func (m *IndexModel) InitModel() string {
    return m.Text
}

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
}

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
}

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)
}

IndexController.ChangeText需要一个指针接收器,或者IndexController.Model需要一个指针。您在 SetText 值的副本上调用 SetText

如果您希望事物是可变的,那么在整个过程中始终使用指向结构的指针并在您真正需要时将显式结构值作为例外会容易得多。