Q:

Unable to set the property of a struct

I'm trying to learn how to use structs in go. 
I have the following package
// src/db/db.go
package db

type DB struct {
  pk string
}

func (db DB) SetPk(s string)  {
  db.pk = s
}
func (db DB) GetPk() string {
  return db.pk
}

And this is my main.go
package main

import (
  "log"
  "db"
)

func main() {

  d := db. DB{}
  d.SetPk("Hello World")
  log. Println(d. GetPk())

}

When I run the command go run main.go, I get a line break in my command prompt.   I verified that both the SetPk and GetPk are being fired, and that the SetPk is assigning a value to db.pk.   But the moment GetPk is fired, db.pk is empty again. 
How do I get the db object to retain the pk value and return it in GetPk? 

A:

It's because you need to make the methods work off of a pointer to the variable and not a copy.  Currently each method only uses a copy of the original (which is blank) to run the method.  Below will work. 
// src/db/db.go
package db

type DB struct {
  pk string
}
// Use the pointer back to the object
func (db *DB) SetPk(s string)  {
  db.pk = s
}
// Use the pointer back to the object
func (db *DB) GetPk() string {
  return db.pk
}

