模型中的setter不起作用。

huangapple go评论93阅读模式
英文:

Setter not working in model

问题

我正在尝试理解MVC架构并学习Go语言,我正在编写这段代码。我遇到了一个问题,即无法从控制器中更改模型中的值。

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

目前在终端上显示的文本如下:

Hello World!
asdf //我的输入
Hello World!

而我希望得到的输出应该是这样的:

Hello World!
asdf //我的输入
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 = 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!",
    }

    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)
}
英文:

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

答案1

得分: 7

IndexController.ChangeText 需要一个指针接收器,或者 IndexController.Model 需要是一个指针。你正在对 SetText 的副本调用 SetText

如果你希望对象是可变的,最好始终使用指针来引用结构体,并且只在确实需要时才使用显式结构体值。

英文:

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.

huangapple
  • 本文由 发表于 2017年4月20日 03:15:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/43504240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定