Use value receiver for slice based type?

Should I use a value receiver for visitInterior? It seems like I should use a pointer receiver to signal the intent that Matrix would be updated in place if there was a mutating function on Matrix, but I’m not sure.

type Matrix [][]int32

func (m Matrix) visitInterior(visit func(r, c int)) {
    for r := 1; r < len(m) - 1; r++ {
        for c := 1; c < len(m) - 1; c++ {
            visit(r, c)
        }        
    }
}

Use a value receiver because the data referred to by a slice are not copied. Making it a pointer just adds noise and makes it a little harder to implement because you must dereference the pointer.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.