dow57588 2017-04-19 19:15
浏览 45
已采纳

设置器在模型中不起作用[重复]

This question already has an answer here:

I am making this code trying to understand the MVC architecture while learning Go, and I am stuck trying to change values in the model from the controller.

The code right now creates a model that only holds a string, the view shows that string on the terminal, but the controller can not change it (it gets the user input without any problem).

Right now the text I get in the terminal is like this:

Hello World!
asdf //my input
Hello World!

And the output I would like to get would be like this:

Hello World!
asdf //my input
asdf

Here are my files:

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('
')
    controller.ChangeText(text)
    fmt.Println(viewIndex.Model.Text)
}

Edit: How my code ended up after @JimB answer:

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('
')
    controller.ChangeText(text)
    fmt.Println(viewIndex.Model.Text)
}
</div>
  • 写回答

1条回答 默认 最新

  • doudui6756 2017-04-19 19:23
    关注

    IndexController.ChangeText needs a pointer receiver, or IndexController.Model needs to be a pointer. You're calling SetText on a copy of the SetText value.

    If you expect things to be mutable, it's much easier to consistently use pointers to structs throughout, and make explicit struct values the exception when you really need them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭