An interface type-assertion asserts that the dynamic value in the interface
(in this case it's `string`) implements the interface you are asserting (in
this case `myinterface`). As `myinterface` has no methods, every value
implements that interface.

In general, a type-assertion will assert things about the dynamic value
stored in an interface. In that code, the dynamic value is `string` *either
way*. In one case, you assign a `string` to `interface{}`, in the other you
assign a `string` to `myinterface`. But both will ultimately store a
`string`. That's also why your first case, using `mystring` works as
expected - in that case, the dynamic type is `string` in one case and
`mystring` in the other (note that the dynamic type is *always* a "concrete
type", not an interface).

In general, ways to get around this is to either a) not use an interface,
or b) give that interface a method only specific types implement, or c)
store a `*myinterface` - it's a pointer, not an interface, thus a concrete
type, which can be distinguished. However, in your case, neither of these
strategies would likely be fruitful. The `json` package needs to know how
to store the values it decodes and it won't be able to, if it doesn't have
a concrete type or a `json.Unmarshaler`.

So in your specific case, you might want to consider using something like

type struct {
    s string
    n int64
    isString bool
}

and have that implement `json.Marshaler` and `json.Unmarshaler`.

On Tue, Feb 16, 2021 at 8:47 AM Santhosh Kumar T <santhosh.tek...@gmail.com>
wrote:

> I have a function call nextRow which returns []interface{}
>
> i want to add support for json, since json value can be string, number
> which conflicts
> with native types, i used following:
>     type Json interface{}
> and returning json values as:
>     return Json(v)
> but in type checking it fails.
>
> i extract minimal code to explain my issue:
>    https://play.golang.org/p/AAbeOzH-SHE
>
> in the above example, it prints: v3 is myinterface
> but v3 is regular interface{} not of type myinterface
>
> could someone help in resolving the issue.
> Is this expected behavior. what can i do to workaround if so
>
> thanks
> Santhosh
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/bcaa91b2-38ab-4713-93c6-34dd55792f4dn%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/bcaa91b2-38ab-4713-93c6-34dd55792f4dn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfGBc1Fqb8yHMQJjYWWMr6REa0x8fsq45t4Gp%2BLOhUU3Dg%40mail.gmail.com.

Reply via email to