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 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用