简体   繁体   中英

In Go, Given an interface{} in which I know what interface it implements, how can I convert to it

My current problem is that I am implementing data-structures and I have written iterators for them. I have visible interfaces for my data-structures, iterators, and all other required objects. In the back I have concrete implementations which I wish to hide from the end user.

This requires many of my functions to return interface{} objects so that I can store any type of object (and leave validation up to the end-user).

One problem I have is that I iterate over a Graph. My iterator{} for my graph implementation returns a concrete vertex type but the Iterator interface returns interface{}. Since the end-users can only my base Vertex interface I have to try to convert to a Vertex interface so they can use it.

Here is the smallest example that I could think of at this point which illustrates my issue:

package main

import (
    "fmt"
    "strconv"
)

type Base interface {
    Required() string
}

type Concrete struct {
    _data int
}
func (con *Concrete) Required() string {
    return strconv.Itoa(con._data)
}

func convert(val interface{}) *Base {
    if con,ok := val.(*Base); ok {
        return con
    }
    return nil
}

func main() {
    conc := new(Concrete)
    conc._data = 5

    base := convert(conc)

    fmt.Println(base)
}

In the code above I really wish that convert would convert the type to *Base. The function convert will return the value nil instead of the lovely value I wish it to be.


Edit: Removed unused code, I thought I had already removed it but I guess not.

Now that I have finished writing this long explanation I had an idea and figured out the solution:

func convert(val interface{}) Base {
    if con,ok := val.(Base); ok {
        return con
    }
    return nil
}

Don't try to cast to a pointer but rather just the Base interface.

I am not sure why this is the case. When I do reflect.TypeOf(val) inside of convert it gives the output

*main.Concrete

I hope that other people find this useful and that someone else can answer the why portion of this answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM