For testing purposes, I'm trying to mock the Rows.Scan method from the database/sql
library. The signature looks like this:
func (r *Rows) Scan(dest ...interface{}) error
Many examples of how to use this show that you pass a pointer to a string (for example) to the Scan method and it will assign the value through the pointer.
var name string
rows.Scan(&name)
I've been trying to recreate something similar for my own understanding and eventually for mocking but it is not working.
func MockScan(args ...interface{}) {
var s2 string
fmt.Println(args[0])
args[0] = &s2
fmt.Println(args[0])
}
func main() {
var s1 string
fmt.Println(&s1)
MockScan(&s1)
fmt.Println(&s1)
}
Results in:
0xc000012950
0xc000012950
0xc000012960
0xc000012950
Where my initial string never takes on its new value. I understand that strings are immutable (although my true understanding of this concept is a little shakey) but somehow the actual Rows.Scan
method manages to mutate the string.