You need two things--main
needs to allocate space for a ps
that input
can write into, which you can do by replacing var v *ps
with v := new(ps)
. The string will be ""
, but it doesn't matter what it is, just that there's space set aside in memory for a string header that input
can write to. As Momer said, otherwise the pointer's nil
and your program panics trying to dereference it.
And in order to assign through a pointer, input
needs to use *s = x
. Since *s
is, informally, "get what s
points to", you can read that as "change what s
points to to x
". Usually the automatic ref/deref behavior around the dot operator and method calls saves you from that, but when you assign through a pointer type or do other operations (arithmetic, indexing, etc.) the dereference needs to be there in the code.