<p>I use the following function, and I need to <strong>raise</strong> the coverage of it (if possible to 100%), the problem is that typically I use <code>interface</code> to handle such cases in Go and for this specific case not sure how to do it, as this is a bit more <strong>tricky</strong>, any idea?</p>
<p><code>The package</code> <a href="https://pkg.go.dev/google.golang.org/genproto/googleapis/cloud/compute/v1" rel="nofollow noreferrer">https://pkg.go.dev/google.golang.org/genproto/googleapis/cloud/compute/v1</a></p>
<p>Which I use doesn't have <code>interface</code> so not sure how can I mock it?</p>
<pre><code>import (
    &quot;context&quot;
    &quot;errors&quot;
    &quot;fmt&quot;
    &quot;os&quot;

    compute &quot;cloud.google.com/go/compute/apiv1&quot;
    &quot;google.golang.org/api/iterator&quot;
    &quot;google.golang.org/api/option&quot;
    computev1 &quot;google.golang.org/genproto/googleapis/cloud/compute/v1&quot;
)

func Res(ctx context.Context, project string, region string,vpc string,secret string) error {

    c, err := compute.NewAddressesRESTClient(ctx, option.WithCredentialsFile(secret))

    if err != nil {
        return err
    }

    defer c.Close()
    addrReq := &amp;computev1.ListAddressesRequest{
        Project: project,
        Region:  region,
    }
    it := c.List(ctx, addrReq)

    for {
        resp, err := it.Next()
        if err == iterator.Done {
            break
        }
        if err != nil {
            return err
        }
        if *(resp.Status) != &quot;IN_USE&quot; {
            return ipConverter(*resp.Name, vpc)
        }
    }
    return nil
}
</code></pre>

<p>Whenever I find myself in this scenario, I found that the easiest solution is to create <em>missing</em> interfaces myself. I limit these interfaces to the types and functions that I am using, instead of writing interfaces for the entire library. Then, in my code, instead of accepting third-party concrete types, I accept my interfaces for those types. Then I use gomock to generate mocks for these interfaces as usual.</p>
<p>The following is a descriptive example inspired by your code.</p>
<pre class="lang-golang prettyprint-override"><code>type RestClient interface {
    List(context.Context, *computev1.ListAddressesRequest) (ListResult, error) // assuming List returns ListResult type.
    Close() error
}

func newRestClient(ctx context.Context, secret string) (RestClient, error) {
    return compute.NewAddressesRESTClient(ctx, option.WithCredentialsFile(secret))
}

func Res(ctx context.Context, project string, region string, vpc string, secret string) error {
    c, err := newRestClient(ctx, secret)
    if err != nil {
        return err
    }

    defer c.Close()
    return res(ctx, project, region, vpc, c)
}

func res(ctx context.Context, project string, region string, vpc string, c RestClient) error {
    addrReq := &amp;computev1.ListAddressesRequest{
        Project: project,
        Region:  region,
    }

    it, err := c.List(ctx, addrReq)
    if err != nil {
        return err
    }

    for {
        resp, err := it.Next()
        if err == iterator.Done {
            break
        }

        if err != nil {
            return err
        }

        if *(resp.Status) != &quot;IN_USE&quot; {
            return ipConverter(*resp.Name, vpc)
        }
    }

    return nil
}
</code></pre>
<p>Now you can test the important bits of the <code>Res</code> function by injecting a mock <code>RestClient</code> to the internal <code>res</code> function.</p>
