Casting to struct in external Module

I am trying to cast a struct in my package to another in an external module

In my package I created

type Userinfo struct {
    username    string
    password    string
    passwordSet bool
}

which is identical to “net/url” Userinfo
though when I try to cast from one to the other like this:

ui := Userinfo{"myusername", "password", true}
url.Userinfo(ui)

I get

cannot convert ui (variable of type Userinfo) to type "net/url".Userinfo

Why can’t I cast to the other struct even though they are completely identical?
If this is generally not possible, is there some hack to do so, I need to convert to the other for compatibility reasons?

(Of course I also imported “net/url” in my package)

Thank you!

I think cast needs to access exported fields but in this case all fields in net/url.Userinfo struct are private. According this, package net/url offers some functions to create Userinfo vars like User() and UserPassword()

HTH,
Yamil

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.