golang单元测试:内部函数参数

I have simple work flow in go functions, but when come to unit test, I'm stuck at passing parameter to inner functions, or mock inner function return results.

code:

package myFunc

import (
    myPackage
    bookPackage
)

func Init() (err error) {
    err = getResource(myPackage.GetPath())
    ...
}

func getResource(path string) (err error) {
    // get resource from path ...
    err := bookPackage.GetBook(path)
}

test:

package myFunc

import "testing"

func TestInit(t *testing.T) {
    if err := Init(); err != nil {
        t.Fatal("test failed")
    }
}

result:

--- FAIL: TestInit (0.00s)
Failed to read path ...

What will be the general solution to this type of scenarios? for example if getResource() calls getBooks() and its parameter comes from another source?

How to mock getBooks() return result, without actually running it?

Thanks,

I'm assuming this isn't real code, since you are calling unexported functions? myPackage.getPath() should never work.

Anyways, the way I tend to do it is to export anything that I need to change in order to test:

package myPackage

var Path string

func init() {
    Path = "/path/to/default/path"
}

func GetPath() {
    return Path
}

and then when you test, you can just override that variable to whatever you need it to be:

package myFunc

import (
    "testing"
    "myPackage"
)

func TestInit(t *testing.T) {
    myPackage.Path = "/test/path"
    if err := Init(); err != nil {
        t.Fatal("test failed")
    }
}