简体   繁体   中英

Indirectly change a value in a struct in Go

I have the following code, feel free to offer pointers if you wish:

package main

import (
  "fmt"
)

type Grid struct {
  rows int
  cols int
  tiles []Tile
}

type Tile struct {
  x int
  y int
  contents int
}

func (g Grid) AddTile(t Tile) {
  g.tiles = append(g.tiles, t)
}

func (g *Grid) Row(num int) []Tile {
  numTiles := len(g.tiles)
  row := []Tile{}
  for i := 0; i < numTiles; i++ {
    tile := g.tiles[i]
    if (tile.y == num) {
      row = append(row, tile)
    }
  }
  return row
}

/*
  HERE IS WHERE I NEED HELP
*/
func (g *Grid) SetRow(num, val int) {
  row := g.Row(num)
  rowLength := len(row)
  for i := 0; i < rowLength; i++ {
    tile := &row[i]
    tile.contents = val
  }
}

func (g Grid) Col(num int) []Tile {
  numTiles := len(g.tiles)
  col := []Tile{}
  for i := 0; i < numTiles; i++ {
    tile := g.tiles[i]
    if (tile.x == num) {
      col = append(col, tile)
    }
  }
  return col
}

func MakeTile(x, y int) Tile {
  tile := Tile{x: x, y: y}
  return tile
}

func MakeGrid(rows, cols int) Grid {
  g := Grid{ rows: rows, cols: cols}
  for r := 1; r <= rows; r++ {
    for c := 1; c <= cols; c++ {
      g.tiles = append(g.tiles, MakeTile(r, c))
    }
  }
  return g
}

func main() {
  g := MakeGrid(256, 256)
  g.SetRow(100, 5)
  fmt.Println(g.Row(100))
}

I am doing this, more than anything, as a simple project to help me learn Go. The problem that is have run in to is here

/*
  HERE IS WHERE I NEED HELP
*/
func (g *Grid) SetRow(num, val int) {
  row := g.Row(num)
  rowLength := len(row)
  for i := 0; i < rowLength; i++ {
    tile := &row[i]
    tile.contents = val
  }
}

Somewhere it seems like I need to be making a pointer to the actual Tiles that I'm trying to modify. As it is the SetRow function doesn't actually modify anything. What am I doing wrong? Keep in mind I just started learning Go 2 days ago, so this is a learning experience :)

One way to accomplish your goal is to use pointers to tiles throughout the code. Change the Grid tiles field to:

tiles []*Tile

and several related changes through the code.

Also, change all the methods to use pointer receivers. The AddTile method as written in the question discards the modification to the grid on return.

playground example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM